DRY方法构造具有相同初始化列表的数组的所有元素?

时间:2016-12-27 15:27:27

标签: c++ arrays c++11 constructor initializer-list

在C ++ 11中,是否有一种DRY方法来构造一个数组的所有元素,并为所有元素设置一组相同的参数? (例如,通过单个初始化列表?)

例如:

class C {
public:
   C() : C(0) {}
   C(int x) : m_x{x} {}
   int m_x;
};

// This would construct just the first object with a parameter of 1.
// For the second and third object the default ctor will be called.
C ar[3] {1};

// This would work but isn't DRY (in case I know I want all the elements in the array to be initialized with the same value.
C ar2[3] {1, 1, 1};

// This is DRYer but obviously still has repetition.
const int initVal = 1;
C ar3[3] {initVal, initVal, initVal};

我知道使用std::vector可以轻松实现目标。我想知道原始数组是否可行。

4 个答案:

答案 0 :(得分:4)

c ++ 14 - 一项小工作将使这项工作适用于c ++ 11

#include <iostream>
#include <array>
#include <utility>

class C {
public:
    C() : C(0) {}
    C(int x) : m_x{x} {}
    int m_x;
};

namespace detail {
    template<class Type, std::size_t...Is, class...Args>
    auto generate_n_with(std::index_sequence<Is...>, const Args&...args)
    {
        return std::array<Type, sizeof...(Is)> {
            {(void(Is), Type { args... })...} // Or replace '{ args... }' with '( args... )'; see in comments below.
        };
    }
}

template<class Type, std::size_t N, class...Args>
auto generate_n_with(const Args&...args)
{
    return detail::generate_n_with<Type>(std::make_index_sequence<N>(), args...);
}

int main()
{
    auto a = generate_n_with<C, 3>(1);
    for (auto&& c : a)
    {
        std::cout << c.m_x << std::endl;
    }
}

结果:

1
1
1
  

我想保证在c ++ 17之前没有副本

你需要生成一个向量:

template<class Container, class...Args>
auto emplace_n(Container& c, std::size_t n, Args const&...args)
{
    c.reserve(n);
    while(n--) {
        c.emplace_back(args...);
    }
};

像这样使用:

std::vector<C> v2;
emplace_n(v2, 3, 1);

答案 1 :(得分:1)

您可以使用stmt = "SELECT * FROM jsonTes ORDER BY website LIMIT %s" %n构造元素序列,并将其扩展为数组的初始值设定项。不过,我不知道任何避免辅助功能的方法。这是一个例子:

std::index_sequence<...>

答案 2 :(得分:1)

通过创建派生类,您可以有效地创建新的默认值。它有点hackish,但可能比其他解决方案更少hackish。这是一个例子:

class C {
public:
   C() : C(0) {}
   C(int x) : m_x{x} {}
   int m_x;
};


template <int init>
struct CInit : C { CInit() : C(init) {} };

CInit<1> ar2[3];


const int initVal = 1;
CInit<initVal> ar3[3];

另一种方法是使用可变参数构造函数将原始数组包装在结构中:

template <size_t n>
struct Array {
    C array[n];

    template <size_t... seq>
    Array(int init,std::index_sequence<seq...>)
    : array{(void(seq),init)...}
    {
    }

    Array(int init)
    : Array(init,std::make_index_sequence<n>())
    {
    }
};


const int initVal = 1;
Array<3> ar3_1(initVal);
const C (&ar3)[3] = ar3_1.array;

答案 3 :(得分:0)

根据理查德的回答,也可以定义

template<class Type, std::size_t N, class...Args>
auto generate_n_with(const std::array<Type, N>&, const Args&...args)
{
    return detail::generate_n_with<Type>(std::make_index_sequence<N>(), args...);
};

允许您输入数组作为参数,以便在您已经知道数组类型的情况下使代码更加干燥,例如

class D {
public:
    D();
    std::array<int, 3> m_ar;
};

允许

D::D() : m_ar{generate_n_with{m_ar, 5}} {}

而不是DRY

D::D() : m_ar{generate_n_with<int, 3>{5}} {}

P.S。也许有一种甚至是DRYer方式而不重复m_ar两次?