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

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. 

主站蜘蛛池模板: 佛冈县| 九江市| 鄂托克旗| 永清县| 潮安县| 石柱| 鄢陵县| 商南县| 平和县| 西昌市| 磴口县| 进贤县| 上思县| 澄迈县| 杂多县| 论坛| 河间市| 南城县| 十堰市| 南丹县| 仁化县| 达孜县| 个旧市| 宜君县| 洞口县| 京山县| 苍山县| 日土县| 保靖县| 阳谷县| 漳浦县| 民和| 永宁县| 泾源县| 比如县| 思南县| 枣庄市| 九龙城区| 凤凰县| 鸡西市| 壤塘县|