- 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.
推薦閱讀
- 計算機組成原理與接口技術:基于MIPS架構實驗教程(第2版)
- Python數據分析入門:從數據獲取到可視化
- Architects of Intelligence
- 算法與數據中臺:基于Google、Facebook與微博實踐
- 數據挖掘原理與SPSS Clementine應用寶典
- 大數據架構商業(yè)之路:從業(yè)務需求到技術方案
- Flutter Projects
- 云數據中心網絡與SDN:技術架構與實現
- Google Cloud Platform for Developers
- 貫通SQL Server 2008數據庫系統(tǒng)開發(fā)
- 大數據技術原理與應用:概念、存儲、處理、分析與應用
- SQL Server 2008寶典(第2版)
- 數據賦能
- Hands-On Deep Learning for Games
- Artificial Intelligence for Big Data