2013-04-06 139 views
4

是否有可能使用任何類型的初始值設定項列表的通用構造函數,即使這個列表中嵌套了列表?嵌套初始值設定項列表的構造函數

假設你有一個類,它在其構造函數嵌套初始化列表下面的模板偏特:

模板 類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弗雷的幫助下,我能製作一個非常通用的構造函數,任何尺寸的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

回答

1

一般來說,答案是(據我所知):不可以,但您的特定情況下,你可能會認爲這一切與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) {} 
}; 

和改變你的構造函數來:

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

+0

不幸的是,這段代碼是未定義行爲,因爲它實例帶有不完整類型的'std :: vector'('arg_type')。將[std :: vector'替換爲[boost :: container :: vector](http://www.boost.org/doc/libs/1_53_0/doc/html/boost/container/vector.html),所有將很好。 – Mankarse 2013-04-06 09:54:39

+0

@Mankarse:謝謝!我使用了無需Boost的替代方案更新了答案。 – 2013-04-06 10:26:06

+0

我很難理解你爲什麼使用矢量。看來你的代碼工作的唯一方式是通過嵌套列表創建一個向量。但是嵌套列表不是可以使用的數據結構嗎? – aaragon 2013-04-06 10:42:51

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

相關問題