typedef改变意思 [英] typedef changes meaning
问题描述
当我用 g ++
template<class T>
class A
{};
template<class T>
class B
{
public:
typedef A<T> A;
};
编译器告诉我
error: declaration of ‘typedef class A<T> B<T>::A’
error: changes meaning of ‘A’ from ‘class A<T>’
另一方面,如果我将 typedef
更改为
On the other hand, if I change the typedef
to
typedef ::A<T> A;
一切编译正确与 g ++
。 Clang ++ 3.1不关心任何一种方式。
everything compiles fine with g++
. Clang++ 3.1 doesn't care either way.
为什么会发生这种情况?是第二个行为标准吗?
Why is this happening? And is the second behavior standard?
推荐答案
g ++是正确的,符合标准。从[3.3.7 / 1]:
g++ is correct and conforming to the standard. From [3.3.7/1]:
在类S中使用的名称N应引用其$ b $中的相同声明b上下文,并且在完成范围内重新评估时。违反此规则,需要
诊断。
A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.
在typedef之前, A
引用 :: A
,但是通过使用typedef, code> A 指的是禁止的typedef。但是,由于无需诊断
,clang也是标准符合。
Before the typedef, A
referred to the ::A
, however by using the typedef, you now make A
refer to the typedef which is prohibited. However, since no diagnostic is required
, clang is also standard conforming.
jogojapan的评论解释了此规则的原因。
对代码进行以下更改:
jogojapan's comment explains the reason for this rule. Take the following change to your code:
template<class T>
class A
{};
template<class T>
class B
{
public:
A a; // <-- What "A" is this referring to?
typedef A<T> A;
};
由于类范围的工作原理, A a;
变得模糊。
Because of how class scope works, A a;
becomes ambiguous.
这篇关于typedef改变意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!