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

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. 

主站蜘蛛池模板: 贵溪市| 兰坪| 沾益县| 顺义区| 古浪县| 富宁县| 新泰市| 乌拉特中旗| 浙江省| 玛曲县| 海安县| 清镇市| 仙居县| 东乡| 常德市| 甘德县| 夏邑县| 鹿泉市| 安顺市| 阜城县| 漯河市| 延边| 奉贤区| 新竹市| 搜索| 米易县| 敦煌市| 商洛市| 鄂托克旗| 安阳市| 阜南县| 临湘市| 饶平县| 阳朔县| 巢湖市| 红桥区| 巩留县| 贡觉县| 贺州市| 浮梁县| 盐亭县|