可变数量的构造函数参数取决于整数模板 [英] Variable number of constructor parameters depending on integer template

查看:101
本文介绍了可变数量的构造函数参数取决于整数模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个容器存储类模板,该模板包装私有的 std :: array 以便为其添加一些功能。模板参数化值的数量,如下所示:

I'm writing a container storage class template that wraps a private std::array in order to add some functionality to it. The template parametrises the number of values, as follows:

template<size_t N> class Vector {
private:
    array<double, N> vals;
public:
    [...]
};

我希望该类的构造函数只接受 N 会加倍以填充数组,但是我找不到一种很好的方法来做到这一点。可变参数不提供一种机制来检查其中有多少个,因此是正确的。参数包不进行浮点升级,但是如果我只能弄清楚如何使用它们,我愿意解决。

I'd like the constructor for the class to only accept N doubles to fill the array, but I can't find a good way to do this. Variadic arguments don't provide a mechanism to check how many of them there are, so they're right out. Parameter packs don't do floating-point promotion, but I'd be willing to deal with that if I could only figure out how to use them for this.

I尝试遵循成员函数模板的参数数量取决于整数模板参数,但我无法理解其含义 enable_if<> :: type = 0 部分。我曾经尝试过天真的复制代码(尽管我宁愿了解它的工作原理。我已经看到人们在其他地方使用 :: value ,但是我可以找不到有关原因的任何文档),但是扩展生成的参数包似乎不起作用。我对参数包的另一个担心是,我不确定它们是否会确保所有参数的类型相同。

I've tried following the approach in the answer to Member function template with the number of parameters depending on an integral template parameter but I can't understand the significance enable_if<>::type=0 section. I've tried naïvely copying that code in (though I'd much rather understand how it works. I've seen people use ::value in other places but I can't find any documentation on why) but expanding the resulting parameter pack doesn't seem to work. My other concern with parameter packs is that I'm not sure that they'd ensure the types of all arguments were the same.

我尝试运行 static_assert 在构造函数的主体中使用初始化列表的大小,但是当然列表的大小在编译时不是恒定的,因此也不起作用。

I've tried running a static_assert on the size of an initializer list, in the body of the constructor, but of course the size of the list is not constant at compile time, so that doesn't work either.

这里是否有标准方法?我只是在使用参数包错误吗?

Is there a standard approach here? Am I just using parameter packs wrong?



更新:
我知道了在我上面链接的答案中,部分起作用:


Update: I've got the approach in the answer I linked above partly working:

template<size_t N> class Vector {
private:
    array<double, N> vals;
public:
    template <typename ...T,
          typename enable_if<sizeof...(T) == N, int>::type = 0>
    Vector(T ...args) {
        vals = {args...};
    }
};

现在的问题是 enable_if 期限模板中的意思是当我初始化 Vector 时,例如,

The issue is now that the enable_if term in the template means that when I initialise a Vector with, for example,

Vector<3> V {1.0, 2.0, 3.0};

它请求模板特化 Vector< 3> :: Vector< double, double,double,0> ,而不是< double,double,double> 。如何摆脱模板中的这个杂项?

It requests a template specialisation Vector<3>::Vector<double, double, double, 0> rather than <double, double, double>. How do I get rid of this stray term in the template?

推荐答案

通过跟随成员函数模板,其参数数量取决于整数模板参数具有正好为n个参数的可变参数模板,可以使用以下代码

By following Member function template with the number of parameters depending on an integral template parameter and Variadic templates with exactly n parameters, have got it working with the following code.

template<size_t N> class Vector {
private:
    array<double, N> vals;
public:
    template <typename ...T,
          typename enable_if<sizeof...(T) == N, int>::type = 0>
    Vector(T ...args) : vals{args} {}
};

这篇关于可变数量的构造函数参数取决于整数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆