가능한 중복 : 당신은 초기화 목록을 잘 성병 :: 배열을 구성 할 수 있습니다
How do I initialize a member array with an initializer_list?이니셜 라이저 목록을 사용하여 std :: array 객체를 생성하는 방법은 무엇입니까?
그러나
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'};
오류 :
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 ++이 사람을 죽이라고 알려줍니다.) –
복사 생성자 등을 덮어 쓰지 않습니까? 'template (std :: decl (...)}}>> enum_addressable_array (E && ... e) : base_t {{std :: forward –
alfC
@alfC 템플릿은 암시 적으로 생성 된 특수 멤버 함수보다 더 정확하게 일치하지 않습니다. 정확하게 일치하는 템플릿이므로 (암시 적으로도 "enum_addressable_array &'와 같은 것을 넘겨 주면 더 좋은 결과를 얻을 수있다. (따라서 과부하 해결을 선호한다.) –