- Hands-On System Programming with C++
- Dr. Rian Quinn
- 146字
- 2021-07-02 14:42:39
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.
推薦閱讀
- Access 2016數據庫教程(微課版·第2版)
- Modern Programming: Object Oriented Programming and Best Practices
- 大數據可視化
- 圖解機器學習算法
- Spark核心技術與高級應用
- 跟老男孩學Linux運維:MySQL入門與提高實踐
- MySQL 8.x從入門到精通(視頻教學版)
- Learning Proxmox VE
- 數據庫原理與設計(第2版)
- LabVIEW 完全自學手冊
- 達夢數據庫運維實戰
- 數據庫與數據處理:Access 2010實現
- 計算機視覺
- 利用Python進行數據分析(原書第2版)
- Deep Learning with R for Beginners