2016-03-26 40 views
2

本質上我想要一個模板類,其大小是一個模板參數,以保持不變的內容。如何從構造函數參數初始化模板成員數組?

是這樣的:

template<size_t S> struct Foo { 
    const int bar[S]; 
    Foo(const int(&par)[S]) : bar(par) { 
     cout << "bar size is " << S << endl; 
    } 
}; 
auto foo = Foo({1,2,3}); 

我一直在尋找和擺弄一點,幾乎都與中間靜態方法實現的解決方法和使用std ::陣列:

template<size_t S> struct Baz { 
    const array<int,S> qux; 
    Baz(const array<int,S>&par) : qux(par) { 
    cout << "size is " << S << endl; 
    } 
}; 
template<size_t S> Baz<S> 
GetBaz(const array<int,S>&in) { 
    return Baz<S>(in); 
} 

int main() { 
    auto sample = GetBaz({1,2,3}); 
    return 0; 
} 

..這已經是一些樣板了,但是std :: array似乎還沒有從初始化列表構造出來? :-(

prog.cpp: In function 'int main()': 
prog.cpp:27:30: error: no matching function for call to 'GetBaz(<brace-enclosed initializer list>)' 
    auto sample = GetBaz({1,2,3}); 
+0

你不能用內置陣列,必須使用'的std :: array' –

+0

對於'自動進樣做到這一點= GetBaz({1,2,3});'它失敗了,因爲你需要指定'GetBaz <5>'或其他。初始化器列表長度不是它們類型的一部分。 –

+1

[這個答案中的代碼](http://stackoverflow.com/a/6114359/1505939)可以幫助你解決你的問題,如果你願意使用'GetBaz(1,2,3)'沒有額外的大括號 –

回答

5

DR1591內置數組邊界現在從支撐,初始化列表可推論,所以:

template<size_t S> struct Baz { 
    const array<int,S> qux; 
    Baz(const array<int,S>&par) : qux(par) { 
    cout << "size is " << S << endl; 
    } 
    Baz(const int (&par)[S]) : qux(std::experimental::to_array(par)) {} 
}; 

template<size_t S> Baz<S> 
GetBaz(const int (&in)[S]) { 
    return Baz<S>(in); 
} 

std::experimental::to_array創建了一個從在內置一個一個std::array。查看鏈接的cppreference頁面以獲得實現。

你可以去內建數組一路,但它有點惱人:

template<size_t S> struct Baz { 
    const int bar[S]; 

    template<size_t... Is> 
    Baz(const int (&par)[S], std::index_sequence<Is...>) 
     : bar { par[Is]... } {} 

    Baz(const int (&par)[S]) : Baz(par, std::make_index_sequence<S>()) {} 
}; 

template<size_t S> Baz<S> 
GetBaz(const int (&in)[S]) { 
    return Baz<S>(in); 
} 
+0

謝謝,我不是使用'實驗性'的粉絲,但你的回答非常明確並回答了問題。 – gatopeich

+0

順便說一句,你知道舊式陣列的等價物嗎?我仍然不明白爲什麼std :: array被添加,而不是改善已經存在的內容。 – gatopeich

+0

@gatopeich請參閱編輯。 –

2

不知道如果我完全理解的問題,這就是你正在努力實現

#include <iostream> 
#include <array> 

template<size_t S> struct Baz { 
    const std::array<int,S> qux; 
    Baz(const std::array<int,S>& par) : qux(par) { 
     std::cout << "size is " << qux.size() << std::endl; 
    } 
}; 

int main() { 
    auto sample = Baz<5>({1,2,3}); // size = 5, values = 1, 2, 3, 0, 0 
    return 0; 
} 

摘要:?

  1. 使用一個std::array代替原始的array。
  2. 指定模板參數,例如:Baz<5>(...)Class template ar沒有推論。
+0

準確地說,我想要推斷數組的大小:-)。我知道類模板參數不能從constructor_推導出來,但我已經準備好爲它使用一個靜態函數GetBaz()。我不能以一種好看的方式解決的問題是將初始化程序列表通過中間函數傳遞給構造函數。 – gatopeich

0

您可以用經典的C數組做到這一點,但使用可變參數的構造函數

#include <array> 
#include <cstddef> 
#include <iostream> 

using namespace std; 

template <size_t S> struct Foo { 
    const int bar[S]; 
    const std::array<int, S> bar2; 

    template <typename ... I> 
     Foo (const I & ... i) : bar {i...}, bar2 {{i...}} 
    { 
     cout << "bar size is " << S << " == " << 
     (sizeof(bar)/sizeof(bar[0])) << " == " << bar2.size() << endl; 
    } 
}; 

int main() 
{ 
    Foo<3> foo {1,2,3}; 

    auto foo2 = Foo<4>{1,2,3,4}; 

    return 0; 
} 
相關問題