可能重複:
How do I initialize a member array with an initializer_list?如何構造具有初始化列表的std :: array對象?
您可以構建一個std ::陣列就好用初始化列表:
std::array<int, 3> a = {1, 2, 3}; // works fine
然而,當我試圖建立它從std::initializer_list
作爲數據成員或類中的基礎對象,它不起作用:
#include <array>
#include <initializer_list>
template <typename T, std::size_t size, typename EnumT>
struct enum_addressable_array : public std::array<T, size>
{
typedef std::array<T, size> base_t;
typedef typename base_t::reference reference;
typedef typename base_t::const_reference const_reference;
typedef typename base_t::size_type size_type;
enum_addressable_array(std::initializer_list<T> il) : base_t{il} {}
reference operator[](EnumT n)
{
return base_t::operator[](static_cast<size_type>(n));
}
const_reference operator[](EnumT n) const
{
return base_t::operator[](static_cast<size_type>(n));
}
};
enum class E {a, b, c};
enum_addressable_array<char, 3, E> ea = {'a', 'b', 'c'};
錯誤用gcc 4.6:
test.cpp: In constructor 'enum_addressable_array<T, size, EnumT>::enum_addressable_array(std::initializer_list<T>) [with T = char, unsigned int size = 3u, EnumT = E]':
test.cpp:26:55: instantiated from here
test.cpp:12:68: error: no matching function for call to 'std::array<char, 3u>::array(<brace-enclosed initializer list>)'
test.cpp:12:68: note: candidates are:
include/c++/4.6.1/array:60:12: note: std::array<char, 3u>::array()
include/c++/4.6.1/array:60:12: note: candidate expects 0 arguments, 1 provided
include/c++/4.6.1/array:60:12: note: constexpr std::array<char, 3u>::array(const std::array<char, 3u>&)
include/c++/4.6.1/array:60:12: note: no known conversion for argument 1 from 'std::initializer_list<char>' to 'const std::array<char, 3u>&'
include/c++/4.6.1/array:60:12: note: constexpr std::array<char, 3u>::array(std::array<char, 3u>&&)
include/c++/4.6.1/array:60:12: note: no known conversion for argument 1 from 'std::initializer_list<char>' to 'std::array<char, 3u>&&'
我怎樣才能得到它的工作,使我的包裝類可以用一個初始化列表來初始化,這樣:
enum_addressable_array<char, 3, E> ea = {'a', 'b', 'c'};
Clang抱怨輸入應該是'E && ... e'。 (有時候,g ++會告訴我要殺人。) –
難道不會覆蓋副本構造函數嗎?有可能限制它更多?,如'模板(std :: declval ()...)...}}> enum_addressable_array(E && ... e):base_t {{std :: forward (e)...}} {}(我無法使它工作) –
alfC
@alfC模板永遠不會比隱式生成的特殊成員函數更好的匹配,因爲它們是完全匹配的(即使它們是隱含的或顯式聲明爲已刪除),但棘手的部分是,如果你傳遞類似'enum_addressable_array&'。 –