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

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. 

主站蜘蛛池模板: 名山县| 永寿县| 嘉禾县| 锡林郭勒盟| 宣城市| 鹤峰县| 大英县| 锡林浩特市| 仪陇县| 自贡市| 三门县| 柘荣县| 社会| 思南县| 宜章县| 锡林郭勒盟| 衡水市| 潼关县| 赤峰市| 恩施市| 通化县| 那曲县| 霍林郭勒市| 万山特区| 偃师市| 双辽市| 鄂伦春自治旗| 宁武县| 昌黎县| 临洮县| 潮州市| 开原市| 柯坪县| 泉州市| 盐源县| 乳源| 广汉市| 乌审旗| 正定县| 平江县| 镇平县|