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

Namespaces

A welcome change to C++17 is the addition of nested namespaces. Prior to C++17, nested namespaces had to be defined on different lines, as follows:

#include <iostream>

namespace X
{
namespace Y
{
namespace Z
{
auto msg = "Hello World\n";
}
}
}

int main(void)
{
std::cout << X::Y::Z::msg;
}

// > g++ scratchpad.cpp; ./a.out
// Hello World

In the preceding example, we define a message that is output to stdout in a nested namespace. The problem with this syntax is obvious—it takes up a lot of space. In C++17, this limitation was removed by giving us the ability to declare nested namespaces on the same line, as follows:

#include <iostream>

namespace X::Y::Z
{
auto msg = "Hello World\n";
}

int main(void)
{
std::cout << X::Y::Z::msg;
}

// > g++ scratchpad.cpp; ./a.out
// Hello World

In the preceding example, we are able to define a nested namespace without the need for separate lines. 

主站蜘蛛池模板: 大埔县| 西藏| 三亚市| 玛沁县| 洛川县| 清远市| 青神县| 溧阳市| 息烽县| 喀喇沁旗| 浪卡子县| 汉寿县| 郸城县| 库伦旗| 台南县| 沙坪坝区| 扬中市| 西安市| 平泉县| 宁都县| 美姑县| 黄龙县| 涟源市| 冀州市| 鹤峰县| 马关县| 白河县| 彭阳县| 苏州市| 广昌县| 隆回县| 通海县| 盐津县| 汉源县| 新源县| 岳普湖县| 桐梓县| 钟山县| 沁水县| 扎赉特旗| 赤城县|