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

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
主站蜘蛛池模板: 睢宁县| 霍州市| 宜兴市| 宝坻区| 青铜峡市| 东源县| 利辛县| 涞水县| 灌阳县| 邯郸市| 长葛市| 忻州市| 车致| 庆元县| 乐东| 突泉县| 卫辉市| 天祝| 繁峙县| 闽侯县| 鄂州市| 南江县| 兴仁县| 凤庆县| 镇安县| 香格里拉县| 灵武市| 清原| 鹤庆县| 璧山县| 梁平县| 深圳市| 桑植县| 三门峡市| 揭阳市| 榆树市| 永兴县| 南陵县| 教育| 西吉县| 驻马店市|