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

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
主站蜘蛛池模板: 威海市| 介休市| 遵义县| 屏东市| 昭苏县| 吉安县| 通城县| 黎川县| 怀来县| 秦安县| 胶州市| 库车县| 宜都市| 巴林左旗| 锦屏县| 太康县| 北川| 开原市| 邵阳县| 博野县| 册亨县| 房山区| 松滋市| 黄冈市| 麻栗坡县| 武安市| 赣榆县| 惠州市| 枝江市| 商河县| 盐城市| 彭泽县| 武平县| 南通市| 临高县| 通化县| 休宁县| 郎溪县| 寻乌县| 莱阳市| 海林市|