typename,type成员和non-type成员:它是有效的代码吗? [英] typename, type members and non-type members: is it valid code?

查看:205
本文介绍了typename,type成员和non-type成员:它是有效的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

struct S {
    struct type {};
    type type;
};

int main() {  
    typename S::type t;
    (void) t;
}

除了这不是一个好主意的事实外,我在阅读了关于SO的另一个问题后正在进行实验.
我发现上面的代码段是 GCC编译没有错误,它是

Apart for the fact that is far from being a good idea, I was experimenting after having read another question here on SO.
I found that the snippet above is compiled with no errors by GCC and it is rejected by clang 3.9 with the following error:

错误:类型名称说明符指向'S'中的非类型成员'type'

error: typename specifier refers to non-type member 'type' in 'S'

我怀疑在这种情况下clang是正确的,而GCC是错误的(实际上,我正在向后者提出问题).
是正确的结论还是typename的有效使用?

I suspect that clang is right in this case and GCC is wrong (actually, I'm opening an issue to the latter).
Is it the right conclusion or is that a valid use of typename?

注意:我不是在问如何解决它,我知道该怎么做.我只问这个代码是否有效.

Note: I'm not asking how to solve it, I know how to do that. I'm asking only if this code is valid or not.

推荐答案

res]/4 :

即使在typename存在的情况下,通常使用合格名称查找来查找 qualified-id .

The usual qualified name lookup is used to find the qualified-id even in the presence of typename.

也就是说,与 elaborated-type-specifier 的情况不同,在这种情况下,名称查找不会忽略非类型名称.

That is, unlike the case with elaborated-type-specifiers, the name lookup in this case does not ignore non-type names.

[temp.res]/3 :

如果 typename-specifier 中的 qualified-id 不表示类型或类模板,则程序格式错误.

If the qualified-id in a typename-specifier does not denote a type or a class template, the program is ill-formed.

所以该程序格式错误.

[temp.res]/4也有一个示例:

[temp.res]/4 also has an example for this:

struct A {
  struct X { };
  int X;
};
struct B {
  struct X { };
};
template<class T> void f(T t) {
  typename T::X x;
}
void foo() {
  A a;
  B b;
  f(b);             // OK: T::X refers to B::X
  f(a);             // error: T::X refers to the data member A::X not the struct A::X
}

这篇关于typename,type成员和non-type成员:它是有效的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆