考虑以下示例:
#include <array>
template <typename T>
struct A {
A() {}
};
typedef A<int> B;
struct S {
std::array<B, 1> b;
S() : b{{B()}} {}
};
int main() {
S s;
}
尝试使用g ++ 4.6.3和-std=c++0x
进行编译时出现此错误:
test.cc: In constructor ‘S::S()’:
test.cc:13:16: error: no matching function for call to ‘std::array<A<int>, 1ul>::array(<brace-enclosed initializer list>)’
test.cc:13:16: note: candidates are:
/usr/include/c++/4.6/array:60:12: note: std::array<A<int>, 1ul>::array()
/usr/include/c++/4.6/array:60:12: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.6/array:60:12: note: constexpr std::array<A<int>, 1ul>::array(const std::array<A<int>, 1ul>&)
/usr/include/c++/4.6/array:60:12: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const std::array<A<int>, 1ul>&’
/usr/include/c++/4.6/array:60:12: note: constexpr std::array<A<int>, 1ul>::array(std::array<A<int>, 1ul>&&)
/usr/include/c++/4.6/array:60:12: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<A<int>, 1ul>&&’
但是,在定义变量时,相同的大括号初始化起作用:
std::array<B, 1> b{{B()}};
是因为这个特定版本的gcc没有完全实现C ++ 11,或者我错过了什么,第一个例子不正确?
答案 0 :(得分:1)
是因为这个特定版本的gcc没有完全实现C ++ 11
最有可能是的。 GCC 4.6已经过时了。
或者我错过了什么,第一个例子不正确?
关于c ++ 11语法,你的例子很好。它在colliru编译,没有错误或警告。