2016-03-26 39 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; 
} 
相关问题