嵌套初始化程序列表的构造函数

时间:2013-04-06 08:43:27

标签: c++ c++11 constructor generic-programming initializer-list

是否可以使用一个通用构造函数来获取任何类型的初始化列表,即使它有嵌套列表?

假设您对接受其构造函数嵌套初始化列表的类具有以下部分模板特化:

模板     class ClassA;

template <>
class ClassA<4> {

  typedef std::initializer_list<double> list_type;
  typedef std::initializer_list<list_type> llist_type;
  typedef std::initializer_list<llist_type> lllist_type;
  typedef std::initializer_list<lllist_type> initializer_type;

  size_t n_[4] = {0};
  double* data_;

public:

  ClassA(initializer_type l) {

    assert(l.size() > 0);
    assert(l.begin()->size() > 0);
    assert(l.begin()->begin()->size() > 0);
    assert(l.begin()->begin()->begin()->size() > 0);

    size_t m = n_[0] = l.size();
    size_t n = n_[1] = l.begin()->size();
    size_t o = n_[2] = l.begin()->begin()->size();
    n_[3] = l.begin()->begin()->begin()->size();

    data_ = new double[m*n*o*n_[3]];

    int i=0, j=0, k=0, p=0;
    for (const auto& u : l) {
      assert(u.size() == n_[1]);
      for (const auto& v : u) {
        assert(v.size() == n_[2]);
        for (const auto& x : v) {
          assert(x.size() == n_[3]);
          for (const auto& y : x) {
            data_[i + m*j + m*n*k + m*n*o*p] = y;
            ++p;
          }
          p = 0;
          ++k;
        }
        k = 0;
        ++j;
      }
      j = 0;
      ++i;
    }
  }

  size_t size() const {
    size_t n = 1;
    for (size_t i=0; i<4; ++i)
      n *= n_[i];
    return n;
  }

  friend std::ostream& operator<<(std::ostream& os, const ClassA& a) {
    for (int i=0; i<a.size(); ++i)
      os<<" "<<a.data_[i];
    return os<<endl;
  }

};


int main()
{

  ClassA<4> TT = { {{{1.}, {7.}, {13.}, {19}}, {{2}, {8}, {14}, {20}}, {{3}, {9}, {15}, {21}}}, {{{4.}, {10}, {16}, {22}}, {{5}, {11}, {17}, {23}}, {{6}, {12}, {18}, {24}}} };
  cout<<"TT -> "<<TT<<endl;

  return 0;
}

此代码打印:

TT ->  1 4 2 5 3 6 7 10 8 11 9 12 13 16 14 17 15 18 19 22 20 23 21 24

现在,我正在尝试概括构造函数,以便我不必为每个维度专门化类模板。 问题是,当我用以下内容替换构造函数时:

template <class L>
ClassA(std::initializer_list<L> l) {
  cout<<"generic list constructor"<<endl;
}

clang编译器因错误而失败:

error: no matching constructor for initialization of 'ClassA<4>

有人能指出为什么会这样吗?模板匹配不适用于初始化程序列表,可能是因为这是一个新的C ++功能? 谢谢大家...

修改

感谢@ JohannesSchaub-litb和@Daniel Frey的帮助,我能够制作一个非常通用的构造函数,它接受任何维度的initializer_list。这是结果代码:

template <int d, typename T>
class ClassA {

  size_t n_[d] = {0};
  T* data_;

  template <int D, typename U>
  struct Initializer_list {

    typedef std::initializer_list<typename Initializer_list<D-1,U>::list_type > list_type;

    Initializer_list(list_type l, ClassA& a, size_t s, size_t idx) {

      a.n_[d-D] = l.size();

      size_t j = 0;
      for (const auto& r : l)
        Initializer_list<D-1, U> pl(r, a, s*l.size(), idx + s*j++);
    }
  };

  template <typename U>
  struct Initializer_list<1,U> {

    typedef std::initializer_list<T> list_type;

    Initializer_list(list_type l, ClassA& a, size_t s, size_t i) {

      a.n_[d-1] = l.size();
      if (!a.data_)
        a.data_ = new T[s*l.size()];

      size_t j = 0;
      for (const auto& r : l)
        a.data_[i + s*j++] = r;
    }
  };

  typedef typename Initializer_list<d,T>::list_type initializer_type;

public:

  // initializer list constructor
  ClassA(initializer_type l) : data_(nullptr) {
    Initializer_list<d, T> r(l, *this, 1, 0);
  }

  size_t size() const {
    size_t n = 1;
    for (size_t i=0; i<4; ++i)
      n *= n_[i];
    return n;
  }

  friend std::ostream& operator<<(std::ostream& os, const ClassA& a) {
    for (int i=0; i<a.size(); ++i)
      os<<" "<<a.data_[i];
    return os<<endl;
  }
};

int main()
{

  ClassA<4, double> TT = { {{{1.}, {7.}, {13.}, {19}}, {{2}, {8}, {14}, {20}}, {{3}, {9}, {15}, {21}}}, {{{4.}, {10}, {16}, {22}}, {{5}, {11}, {17}, {23}}, {{6}, {12}, {18}, {24}}} };
  cout<<"TT -> "<<TT<<endl;

  return 0;
}

当然代码打印

TT ->  1 4 2 5 3 6 7 10 8 11 9 12 13 16 14 17 15 18 19 22 20 23 21 24

我喜欢这个模板元编程的东西! 谢谢大家帮忙解决这个问题。

AA

2 个答案:

答案 0 :(得分:5)

我相信你真正想做的是自动构建正确的类型

template<int S, typename E>
class make_list_type {
public:
  typedef std::initializer_list<
    typename make_list_type<S-1, E>::type
  > type;
};

template<typename E>
class make_list_type<0, E> {
public:
  typedef E type;
};

template<int S>
class ClassA {
  typedef typename make_list_type<S, double>::type initializer_type;

public:
  ClassA(initializer_type l) 
};

至于为什么您的尝试不起作用,请参阅Templates don't always guess initializer list types

答案 1 :(得分:1)

一般来说,答案是(AFAIK):不是。但是对于您的具体案例,您可能会使用以double为结尾的知识作为叶子:

class arg_type
{
private:
    bool is_value;
    double d;
    std::vector<arg_type> subs;
public:
    arg_type(double v) : is_value(true), d(v) {}
    arg_type(std::initializer_list<arg_type> l) : is_value(false), subs(l) {}
};

并将您的ctor更改为:

typedef std::initializer_list<arg_type> initializer_type;

ClassA(initializer_type l) {
  // ...
}

希望它有所帮助...


更新:正如Mankarse所指出的那样(谢谢!),上面有未定义的行为。这是一个应该修复它而不使用Boost的版本:

#include <vector>
#include <memory>
#include <iostream>
#include <initializer_list>

class arg_type
{
private:
    std::shared_ptr<void> subs; // empty => d is valid
    double d;

public:
    arg_type(double v) : d(v) {}
    arg_type(std::initializer_list<arg_type> l);

    void print() const;
};

arg_type::arg_type(std::initializer_list<arg_type> l)
  : subs(std::make_shared<std::vector<arg_type>>(l))
{}

void arg_type::print() const
{
   if( subs ) {
     std::cout << "( ";
     for( const auto& e : *std::static_pointer_cast<std::vector<arg_type>>(subs) ) {
       e.print();
     }
     std::cout << ") ";
   }
   else {
      std::cout << d << " ";
   }
}

struct MyClass
{
   MyClass( std::initializer_list<arg_type> l) {
      for( const auto& e : l ){
         e.print();
      }
   }
};

int main()
{
   MyClass m { 1, 2, { 3, 4, { 6, 7, { 8 }}}, 5 };
}

如果你想玩它,这里是live example