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

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
主站蜘蛛池模板: 拜城县| 思茅市| 梁河县| 永丰县| 莫力| 新巴尔虎右旗| 吉木萨尔县| 余江县| 景泰县| 江孜县| 福海县| 漳平市| 临湘市| 九龙城区| 竹溪县| 泾川县| 乳源| 江阴市| 祁东县| 巴南区| 吴堡县| 隆昌县| 巧家县| 乐至县| 武威市| 朝阳区| 曲阳县| 佛教| 区。| 民和| 同仁县| 济阳县| 阿巴嘎旗| 喀喇沁旗| 昆明市| 高州市| 上栗县| 精河县| 鄢陵县| 阿克陶县| 靖边县|