如何用初始化列表构造std :: array对象?

HC4*_*ica 29 c++ arrays initializer-list c++11

可能重复:
如何使用initializer_list初始化成员数组?

您可以使用初始化列表构建一个std :: array:

std::array<int, 3> a = {1, 2, 3};  // works fine
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试从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'};
Run Code Online (Sandbox Code Playgroud)

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>&&'
Run Code Online (Sandbox Code Playgroud)

我怎样才能使它工作,以便我的包装类可以用初始化列表初始化,如下:

enum_addressable_array<char, 3, E> ea = {'a', 'b', 'c'};
Run Code Online (Sandbox Code Playgroud)

Joh*_*itb 30

一个std::array<>没有构造函数的std::initializer_list<>(初始化列表构造器),有什么可能意味着通过一个没有特殊的语言支持std::initializer_list<>的类的构造函数,使得可以正常工作.所以失败了.

为了使它工作,你的派生类需要捕获所有元素,然后转发它们,一个构造函数模板:

template<typename ...E>
enum_addressable_array(E&&...e) : base_t{{std::forward<E>(e)...}} {}
Run Code Online (Sandbox Code Playgroud)

请注意,{{...}}在这种情况下你需要因为大括号(省略大括号)在那个地方不起作用.它只允许在表单声明中使用T t = { ... }.因为它std::array<>包含一个嵌入原始数组的结构,所以需要两个级别的大括号​​.不幸的是,我认为确切的聚合结构std::array<>是未指定的,因此您需要希望它适用于大多数实现.

  • Clang抱怨输入应该是'E && ... e`.(有时,g ++告诉我要杀人.) (4认同)

Jas*_*son 25

由于a std::array是包含聚合的结构(它本身不是聚合,并且没有构造函数采用a std::initializer_list),因此可以使用双括号语法初始化结构内部的基础聚合,如下所示:

std::array<int, 4> my_array = {{1, 2, 3, 4}};
Run Code Online (Sandbox Code Playgroud)

请注意,这不是使用std::initializer_list...这只是使用C++初始化程序列表来初始化可公开访问的数组成员std::array.


R. *_*des 10

An std::array没有构造函数std::initializer_list.这是一件好事,因为初始化列表可能比数组的固定大小更大.

您可以通过测试初始化​​程序列表不大于数组大小然后将初始化程序列表的元素复制到with 的elems成员来初始化std::arraystd::copy.