为什么只需要在全局命名空间中包含 vector(以及其他 headers)?
Why it's required to include vector (and other headers too) in global namespace only?
这是我的示例代码及其输出。
namespace AAA
{
#include <vector>
}
int main()
{
AAA::std::vector<int> vec;
return 0;
}
/usr/include/c++/4.9/new:129:41: error: 'void* AAA::operator new(AAA::std::size_t)' may not be declared within a namespace
__attribute__((__externally_visible__));
- 为什么不允许在命名空间中声明运算符 new/delete?
- 标准是否要求从全局命名空间中包含
<vector>
(以及其他 headers)?
是的,此运算符必须在全局范围内。是 "special".
[C++14: 3.7.4.1/1]:
An allocation function shall be a class member function or a global function; a program is ill-formed if an allocation function is declared in a namespace scope other than global scope or declared static in global
scope. [..]
而且,是的,标准库头文件必须是 #include
d in "free space":
[C++14: 17.6.2.2/3]:
A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header. No diagnostic is required.
这是我的示例代码及其输出。
namespace AAA
{
#include <vector>
}
int main()
{
AAA::std::vector<int> vec;
return 0;
}
/usr/include/c++/4.9/new:129:41: error: 'void* AAA::operator new(AAA::std::size_t)' may not be declared within a namespace
__attribute__((__externally_visible__));
- 为什么不允许在命名空间中声明运算符 new/delete?
- 标准是否要求从全局命名空间中包含
<vector>
(以及其他 headers)?
是的,此运算符必须在全局范围内。是 "special".
[C++14: 3.7.4.1/1]:
An allocation function shall be a class member function or a global function; a program is ill-formed if an allocation function is declared in a namespace scope other than global scope or declared static in global scope. [..]
而且,是的,标准库头文件必须是 #include
d in "free space":
[C++14: 17.6.2.2/3]:
A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header. No diagnostic is required.