带有使用声明链接的程序在MSVS和clang上编译,但不在GCC上编译 [英] Program with chaining of using-declarations compiles on MSVS and clang but not on GCC
问题描述
以下程序是否根据c ++标准格式正确或格式不正确?
Is the following program well-formed or ill-formed according to the c++ standard?
namespace X { int i; }
namespace Y { using X::i; }
int main() { using X::i; using Y::i; }
我用不同的编译器得到不同的结果:
I'm getting different results with different compilers:
- MSVS:编译( http://webcompiler.cloudapp.net/ )
- Clang:编译( http://melpon.org/wandbox/permlink/KloDufJ5h1DalK4v )
- GCC:编译错误( http://melpon.org/wandbox/permlink/IKuuQGE1THofuUTr) )
- MSVS: Compiles ( http://webcompiler.cloudapp.net/ )
- Clang: Compiles ( http://melpon.org/wandbox/permlink/KloDufJ5h1DalK4v )
- GCC: Compile error ( http://melpon.org/wandbox/permlink/IKuuQGE1THofuUTr )
我不想修复此程序,使其在GCC上编译。我只想知道c ++标准关于这一点以及为什么三个编译器的行为不同。
I don't want to fix this program to make it compile on GCC. I just want to know what the c++ standard says about this and why the three compilers behave differently. Also I want to if this is a result of a bug in any of these compilers.
推荐答案
Clang和MSVC是正确的;如果这是一个错误的结果是任何这些编译器。此代码有效。如Alf所说, [namespace.udecl](7.3.3)/ 10 说
Clang and MSVC are correct; this code is valid. As Alf notes, [namespace.udecl] (7.3.3)/10 says
use-declaration 是一个声明 ,因此可以在允许多个声明的地方重复使用。
A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed.
但是,对块范围中的同一实体的多个声明没有限制,因此原始示例有效。不涉及 using-declaration 的相应情况是:
However, there is no restriction on multiple declarations of the same entity in block scope, so the original example is valid. A corresponding case not involving using-declarations is:
int n;
void f() {
extern int n;
extern int n;
}
这是有效的(并且被GCC,EDG,Clang和MSVC接受),因此(通过上面引用的规则)原始示例也是有效的。
This is valid (and is accepted by GCC, EDG, Clang, and MSVC), therefore (by the above-quoted rule) the original example is also valid.
值得注意的是, [namespace.udecl] 7.3.3)/ 10 包含错误。它说:
It is worth noting that the example in [namespace.udecl] (7.3.3)/10 contains an error. It says:
namespace A {
int i;
}
void f() {
using A::i;
using A::i; // error: double declaration
}
...但注释不正确;第二个声明没有错误。请参阅核心问题36 中的讨论。我已从标准中移除示例,这样就不会混淆更多人。
... but the comment is not correct; there is no error on the second declaration. See the discussion in core issue 36. I've removed the example from the standard so that it won't confuse more people.
这篇关于带有使用声明链接的程序在MSVS和clang上编译,但不在GCC上编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!