【发布时间】:2016-06-24 10:00:51
【问题描述】:
在clang/llvm 3.6.2中,使用std=c++11编译时,以下代码会导致编译错误:
template <typename T=void>
class bar
{
public:
struct foo
{
int array[10];
};
int baz()
{
return sizeof(foo::array);
}
};
int main(void)
{
bar<> b;
return b.baz();
}
命令行调用:
$ clang++ -std=c++11 nonstatic.cpp -o nonstatic
nonstatic.cpp:12:28: error: invalid use of non-static data member 'array'
return sizeof(foo::array);
~~~~~^~~~~
nonstatic.cpp:20:14: note: in instantiation of member function
'bar<void>::baz' requested here
return b.baz();
如果我将bar 更改为不再是模板,如
class bar
{
public:
struct foo
{
int array[10];
};
int baz()
{
return sizeof(foo::array);
}
};
int main(void)
{
bar b;
return b.baz();
}
然后代码编译干净。值得注意的是,GCC 5.2.1 接受std=c++11 下的两个版本。另外值得注意的是,将 array 移动到封闭类模板主体(但将其保留为模板)也会导致 clang 接受这一点。
相对于标准,哪种行为是正确的?这是 GCC、clang 中的错误,还是两者都有?
(我在 cfe-users 上问过同样的question,但到目前为止没有收到任何回复)。
【问题讨论】:
-
看起来像一个 clang++ 错误。等待大师确认。
-
是的,当然是一个错误。
-
stackoverflow.com/questions/29359661/… 中提到的解决方法
sizeof(((foo*) 0)->array)也适用于此。
标签: c++11 gcc clang language-lawyer clang++