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

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
主站蜘蛛池模板: 扎赉特旗| 石渠县| 贡山| 安龙县| 平遥县| 七台河市| 武定县| 扎赉特旗| 六安市| 同江市| 和龙市| 长宁区| 新干县| 永吉县| 辉南县| 高清| 蒲江县| 兰西县| 卓尼县| 鸡西市| 阿拉尔市| 清丰县| 阿鲁科尔沁旗| 图木舒克市| 如皋市| 分宜县| 永胜县| 东城区| 哈尔滨市| 仙桃市| 青岛市| 钟祥市| 隆化县| 定南县| 彭水| 钟山县| 开封县| 文登市| 丁青县| 湖北省| 兴义市|