模板静态成员的初始化 class
initialization of static member of template class
考虑以下因素:
template<class T>
struct A
{
struct B
{} static const b;
};
// case 1
template<class T>
typename A<T>::B const A<T>::b;
// case 2
template<class T>
typename A<T>::B const A<T>::b{};
int main()
{
A<int> a;
a.b;
return 0;
}
案例 1:
gcc 5.2 通过
msvc 2015 更新 1 次通过
clang 3.7 错误:
default initialization of an object of const type 'const typename A<int>::B' without a user-provided default constructor
case2:
gcc 5.2 通过
clang 3.7 通过:
msvc 2015 更新 1 错误:
error C2143: syntax error: missing ';' before '<end Parse>'
每种情况下哪个编译器是正确的/错误的?
typename A<T>::B
是类型
A<T>::
范围
const A<T>::
它是一个 const 作用域吗?
应该是:
const typename A<T>::B A<T>::b;
根据 C++ 标准(8.5 初始化程序)
- ...如果程序要求默认初始化 const 限定类型 T 的对象,则 T 应为 class 类型,具有
用户提供的默认构造函数.
因此 clang 正确地报告了 diagnostoc 消息。
对于第二种情况,使用的MS编译器似乎不支持列表初始化或存在错误。
考虑以下因素:
template<class T>
struct A
{
struct B
{} static const b;
};
// case 1
template<class T>
typename A<T>::B const A<T>::b;
// case 2
template<class T>
typename A<T>::B const A<T>::b{};
int main()
{
A<int> a;
a.b;
return 0;
}
案例 1:
gcc 5.2 通过
msvc 2015 更新 1 次通过
clang 3.7 错误:
default initialization of an object of const type 'const typename A<int>::B' without a user-provided default constructor
case2:
gcc 5.2 通过
clang 3.7 通过:
msvc 2015 更新 1 错误:
error C2143: syntax error: missing ';' before '<end Parse>'
每种情况下哪个编译器是正确的/错误的?
typename A<T>::B
是类型
A<T>::
范围
const A<T>::
它是一个 const 作用域吗?
应该是:
const typename A<T>::B A<T>::b;
根据 C++ 标准(8.5 初始化程序)
- ...如果程序要求默认初始化 const 限定类型 T 的对象,则 T 应为 class 类型,具有 用户提供的默认构造函数.
因此 clang 正确地报告了 diagnostoc 消息。
对于第二种情况,使用的MS编译器似乎不支持列表初始化或存在错误。