- Mastering C++ Programming
- Jeganathan Swaminathan
- 208字
- 2021-07-02 18:28:46
Easier nested namespace syntax
Until the C++14 standard, the syntax supported for a nested namespace in C++ was as follows:
#include <iostream>
using namespace std;
namespace org {
namespace tektutor {
namespace application {
namespace internals {
int x;
}
}
}
}
int main ( ) {
org::tektutor::application::internals::x = 100;
cout << "\nValue of x is " << org::tektutor::application::internals::x << endl;
return 0;
}
The preceding code can be compiled and the output can be viewed with the following commands:
g++-7 main.cpp -std=c++17
./a.out
The output of the preceding program is as follows:
Value of x is 100
Every namespace level starts and ends with curly brackets, which makes it difficult to use nested namespaces in large applications. C++17 nested namespace syntax is really cool; just take a look at the following code and you will readily agree with me:
#include <iostream>
using namespace std;
namespace org::tektutor::application::internals {
int x;
}
int main ( ) {
org::tektutor::application::internals::x = 100;
cout << "\nValue of x is " << org::tektutor::application::internals::x << endl;
return 0;
}
The preceding code can be compiled and the output can be viewed with the following commands:
g++-7 main.cpp -std=c++17
./a.out
The output remains the same as the previous program:
Value of x is 100
推薦閱讀
- 程序員面試白皮書
- JavaScript全程指南
- Developing Middleware in Java EE 8
- 數(shù)據(jù)結(jié)構(gòu)簡明教程(第2版)微課版
- Python機(jī)器學(xué)習(xí)經(jīng)典實(shí)例
- PHP+Ajax+jQuery網(wǎng)站開發(fā)項(xiàng)目式教程
- 并行編程方法與優(yōu)化實(shí)踐
- 單片機(jī)原理及應(yīng)用技術(shù)
- ASP.NET開發(fā)寶典
- Raspberry Pi開發(fā)實(shí)戰(zhàn)
- Learning Puppet
- Building Microservices with .NET Core 2.0(Second Edition)
- R High Performance Programming
- Netty 4核心原理與手寫RPC框架實(shí)戰(zhàn)
- Learning C# by Developing Games with Unity 3D Beginner's Guide