官术网_书友最值得收藏!

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
主站蜘蛛池模板: 邮箱| 高雄县| 赤峰市| 平江县| 慈利县| 阳信县| 云梦县| 荆州市| 松溪县| 宁城县| 怀化市| 喀喇沁旗| 桃园市| 策勒县| 梅州市| 汪清县| 贵阳市| 嘉峪关市| 商城县| 康马县| 南康市| 昌宁县| 保亭| 麻江县| 阜城县| 将乐县| 丰顺县| 东方市| 鹤山市| 都兰县| 伊金霍洛旗| 贵德县| 新建县| 肥城市| 崇左市| 莱州市| 崇仁县| 安福县| 天长市| 马鞍山市| 新昌县|