我注意到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;
};
Run Code Online (Sandbox Code Playgroud)
两个编译器似乎在sizeof(sizeof(T))类型依赖或依赖于值的表达式上有所不同.如果表达式是依赖的,那么它I<sizeof(sizeof(T))>是一个依赖类型,这意味着typename应该是必需的.
这由C++ 11标准中的以下措辞涵盖:
[temp.dep.type]/8
如果是,则类型依赖于
- 一个simple-template-id,其中模板名称是模板参数或任何模板参数是依赖类型或依赖于类型或依赖于值的表达式
[temp.dep.expr/4
以下表单的表达式从不依赖于类型(因为表达式的类型不能依赖):
Run Code Online (Sandbox Code Playgroud)sizeof unary-expression sizeof ( type-id )[temp.dep.constexpr]/2
如果unary-expression或expression是typedependent或type-id是依赖的,则以下形式的表达式是值依赖的:
Run Code Online (Sandbox Code Playgroud)sizeof unary-expression sizeof ( type-id )
我的解释是,sizeof(T)永远不能依赖于类型,意味着sizeof(sizeof(T))永远不能依赖于类型或依赖于价值.
这是gcc中的错误吗?
我正在尝试使用 clang 在类范围之外定义模板类的静态变量:
class Bar
{
public:
float a;
};
template<long count>
class Foo {
public:
static Bar* test;
};
template<long count>
decltype(Foo<count>::test) Foo<count>::test; // error
int main() {
Foo<5> f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
error: redefinition of 'test' with a different type: 'decltype(Foo<count>::test)' vs 'Bar *'
在我看来decltype(Foo<count>::test)应该评估为Bar *.
这段代码在 MSVC 上运行良好。
我的问题:有没有办法decltype在这里正确确定类型?
在实际代码中,decltype是在一个宏中定义的,该宏具有一些附加关键字,具体取决于所使用的配置,所以我希望在仍然使用decltype.