防止将命名空间劫持到内联命名空间
Prevent hijacking a namespace to an inline namespace
必须将命名空间声明为 inline
是第一个,或者任何(重新打开的)命名空间都可以在任何时候声明为 inline
吗?
如果是这样,有没有办法阻止某人将命名空间声明为 inline
(之后)?
#include <iostream>
namespace outer {
//inline
namespace inner {
}}
//inline namespace std {}
namespace outer {
inline
namespace inner {
void foo() {
std::cout << "foo" << std::endl;
//::cout << "foo" << endl;
}
}}
int main() {
outer::foo();
}
编辑
gcc 似乎接受了这一点,因为我首先只在 coliru 上进行了测试,但是 clang rejects it
这是一个错误吗?
7.3.1/7 告诉我们必须在第一个声明处声明内联:
If the optional initial inline keyword appears in a
namespace-definition for a particular namespace, that namespace is
declared to be an inline namespace. The inline keyword may be used on
an extension namespace-definition only if it was previously used on
the original-namespace-definition for that namespace.
必须将命名空间声明为 inline
是第一个,或者任何(重新打开的)命名空间都可以在任何时候声明为 inline
吗?
如果是这样,有没有办法阻止某人将命名空间声明为 inline
(之后)?
#include <iostream>
namespace outer {
//inline
namespace inner {
}}
//inline namespace std {}
namespace outer {
inline
namespace inner {
void foo() {
std::cout << "foo" << std::endl;
//::cout << "foo" << endl;
}
}}
int main() {
outer::foo();
}
编辑
gcc 似乎接受了这一点,因为我首先只在 coliru 上进行了测试,但是 clang rejects it
这是一个错误吗?
7.3.1/7 告诉我们必须在第一个声明处声明内联:
If the optional initial inline keyword appears in a namespace-definition for a particular namespace, that namespace is declared to be an inline namespace. The inline keyword may be used on an extension namespace-definition only if it was previously used on the original-namespace-definition for that namespace.