我正在尝试使用initializer list constructor初始化data_
私有类数据成员,该成员是长度为3的C ++ 11数组:
template<typename Type>
class triElement
{
public:
triElement(std::initializer_list<Type> list)
:
data_(list)
{}
auto begin() const
{
return data_.begin();
}
auto end() const
{
return data_.end();
}
private:
std::array<Type, 3> data_;
};
complete code example的编译因错误而失败:
g++ -std=c++1y -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In instantiation of 'triElement<Type>::triElement(const std::initializer_list<_Tp>&) [with Type = int]':
main.cpp:80:31: required from here
main.cpp:37:27: error: no matching function for call to 'std::array<int, 3ul>::array(const std::initializer_list<int>&)'
data_(list)
即使属性data
属于std :: vector类型,完全相同的方法也适用:
template<typename Type>
class elements
{
public:
elements(std::initializer_list<Type> list)
:
data_(list)
{}
auto begin() const
{
return data_.begin();
}
auto end() const
{
return data_.end();
}
private:
std::vector<Type> data_;
};
在将初始化列表构造函数调用委托给C ++ 11数组属性时,是否需要考虑一些特殊内容?