我注意到gcc 5.0拒绝以下代码,而clang 3.6接受它。
template<int n>
struct I
{
typedef int Type;
};
template<typename T>
struct A
{
typedef I<sizeof(sizeof(T))>::Type Type;
};
两个编译器似乎在
sizeof(sizeof(T))
是否为类型依赖或值依赖表达式上存在差异。如果该表达式是依赖的,则可以得出 I<sizeof(sizeof(T))>
是一个依赖类型,这意味着需要使用 typename
。C++11 标准中包含以下措辞来涵盖此内容:
[temp.dep.type]/8
A type is dependent if it is
- a simple-template-id in which either the template name is a template parameter or any of the template arguments is a dependent type or an expression that is type-dependent or value-dependent
[temp.dep.expr]/4
Expressions of the following forms are never type-dependent (because the type of the expression cannot be dependent):
sizeof unary-expression sizeof ( type-id )
[temp.dep.constexpr]/2
Expressions of the following form are value-dependent if the unary-expression or expression is typedependent or the type-id is dependent:
sizeof unary-expression sizeof ( type-id )
我的理解是sizeof(T)
永远不可能依赖于类型,这意味着sizeof(sizeof(T))
永远不可能依赖于类型或值。
这是gcc的一个bug吗?
sizeof n
是相关的。http://melpon.org/wandbox/permlink/BLobLBzkQXNRfDuq - willjsizeof(type-id)
仍然不是类型相关的。 - dypsizeof n
不受值限制,但sizeof(T)
是受限于其类型的。这个新版本需要再加上一个sizeof()
才能不再受限。 - willj