您可以使用初始化列表构建一个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, …为什么这样做:
std::pair<int, int> p = {1,2};
std::vector<std::pair<int, int>> vp = { {1,2}, {3,4} };
但这不是吗?
std::array<int, 2> a = {1,2}; // still ok
std::vector<std::array<int, 2>> va = { {1,2}, {3,4} };
使用g ++ 4.5.1 -std=c++0x,第二行失败:
错误:无法转换
‘{{1, 2}, {3, 4}}’为‘std::vector<std::array<int, 2u> >’
谢谢
可变构造函数是否应该隐藏隐式生成的构造函数,即默认构造函数和复制构造函数?
struct Foo
{
    template<typename... Args> Foo(Args&&... x)
    {
        std::cout << "inside the variadic constructor\n";
    }
};
int main()
{
    Foo a;
    Foo b(a);
}
不知何故,我希望在阅读这个答案之后不打印任何内容,但它会inside the variadic constructor在g ++ 4.5.0上打印两次:(这种行为是否正确?
它也没有可变参数模板:
struct Foo
{
    Foo()
    {
        std::cout << "inside the nullary constructor\n";
    }
    template<typename A> Foo(A&& x)
    {
        std::cout << "inside the unary constructor\n";
    }
};
int main()
{
    Foo a;
    Foo b(a);
}
同样,两行都打印出来.
我的情况可以归纳如下:
class Test
{
    Test();
    int MySet[10];
};
是否可以MySet在初始化列表中初始化?
像这种初始化列表:
Test::Test() : MySet({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {}
有没有办法在类的初始化列表中初始化一个常量大小的成员数组?
在C++ 0x(哦!读取C++ 11)中,我们有自动类型推断.令我好奇的一件事是我无法创建一系列自动变量.例如:
auto A[] = {1, 2, 3, 4}; // Error!
有什么想法可能被禁止了吗?
我可以使用std::initializer_list对象而不是大括号括起初始化器来初始化数组吗?
众所周知,我们可以这样做:http://en.cppreference.com/w/cpp/language/aggregate_initialization
unsigned char b[5]{"abc"};
// equivalent to unsigned char b[5] = {'a', 'b', 'c', '\0', '\0'};
int ar[] = {1,2,3};
std::array<int, 3> std_ar2{ {1,2,3} };    // std::array is an aggregate
std::array<int, 3> std_ar1 = {1, 2, 3};
但我不能通过以下方式初始化数组std::initializer_list il;:
#include <iostream>
#include <initializer_list>
#include <array>
int main() {
    int arr1[] =  { 1, 2, 3 };  // OK
    std::array<int, 3> arr2 =  { 1, 2, 3 }; // OK
    std::initializer_list<int> il …它可能吗?
#include <array>
#include <initializer_list>
struct A
{
    A ( std::initializer_list< int > l )
        : m_a ( l )
    {
    }
    std::array<int,2> m_a;
};
int main()
{
    A a{ 1,2 };
}
但是这导致了这个错误:
t.cpp: In constructor ‘A::A(std::initializer_list<int>)’:
t.cpp:7:19: error: no matching function for call to ‘std::array<int, 2ul>::array(std::initializer_list<int>&)’
         : m_a ( l )
                   ^
t.cpp:7:19: note: candidates are:
In file included from t.cpp:1:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/include/g++-v4/array:81:12: note: std::array<int, 2ul>::array()
     struct array
            ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/include/g++-v4/array:81:12: note:   candidate expects 0 arguments, 1 provided
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/include/g++-v4/array:81:12: note: …我需要使用initializer_list初始化一个编译时大小的类数组。我已经知道我可以使用参数包构造函数并当场对其进行初始化,但是在这种情况下,我需要使用initializer_list。我还想避免在可能的情况下动态初始化数组。这是伪代码:
template<typename Type, std::size_t Length>
class Test
{
    public:
        Test(const std::initializer_list<Type> args)
        :   m_tData(args) //<-- Doesn't work of course
        {}
    private:
        Type m_tData[Length];
};
当然,对于非const类型,我可以
Test(const std::initializer_list<Type> args)
{
    std::copy(args.start(), args.end(), m_tData);
}
但是如果我尝试使用像这样的const类,这是行不通的Test<const int, 3>({1, 2, 3})。我是否被迫使用模板专门化来使内部数组实际上不是const,以便可以从构造函数主体中对其进行初始化?
我正在使用C ++ 20。我看过如何使用initializer_list初始化成员数组?但想知道从那以后是否有任何变化。
考虑以下代码:
struct foo {
  std::vector<int> v;
  foo(std::initializer_list<int> L) : v{L} {}
};
上面的代码编译正常并按v预期初始化.现在考虑以下代码:
struct bar {
  std::array<int, 3> a;
  bar(std::initializer_list<int> L) : a{L} {}
};
上面的代码给出了编译错误.
错误:没有可行的从'std :: initializer_list'到'int'的转换
在网上搜索我发现std::array使用a 初始化成员的"正确"方法std::list_initializer是以reinterpret_cast下列方式使用:
bar(std::initializer_list<int> L) : a(reinterpret_cast<std::array<int, 3> const&>(*(L.begin()))) {}
为什么我可以在构造函数的初始化列表中初始化一个成员std::vector,std::initializer_list但我不能成为成员std::array?
是工作,各地表现出上面reinterpret_cast初始化成员的正确方法std::array有std::initializer_list?