由于还没有报价,我一直在玩你的例子:
gcc 4.5.1 和 Clang 3.0 都接受代码,如下所示。
现在,我们只需要有人挖掘出一个权威的答案。尽管 Clang、gcc 和 VC++ 达成一致(不是那么频繁),但这似乎是有意的。
在ideone (4.5.1) 上:
#include <utility>
struct A
{
struct Gold {};
};
struct B : public A
{
typedef Gold BaseGold;
struct Gold {};
};
struct C : public B
{
typedef Gold BaseGold;
struct Gold {};
};
static_assert(std::is_same<B::BaseGold, A::Gold>::value, "Not the right treasure!");
static_assert(std::is_same<C::BaseGold, B::Gold>::value, "Not the right treasure!");
在铿锵声:
#include <stdio.h>
template <typename T, typename U>
struct is_same { enum { value = false }; };
template <typename T>
struct is_same<T,T> { enum { value = true }; };
struct A
{
struct Gold {};
};
struct B : public A
{
typedef Gold BaseGold;
struct Gold {};
};
struct C : public B
{
typedef Gold BaseGold;
struct Gold {};
};
int main() {
if (!is_same<B::BaseGold, A::Gold>::value) {
printf("oups");
}
if (!is_same<C::BaseGold, B::Gold>::value) {
printf("oups");
}
}
Clang 输出(如预期):
define i32 @main() nounwind readnone {
entry:
ret i32 0
}