【发布时间】:2020-05-26 16:49:51
【问题描述】:
以下是我目前的实现:
struct Dual {
float v;
std::valarray<float> d;
Dual(float v, std::valarray<float> d): v(v), d(d) {}
Dual(float v, float d = 0.f): v(v), d({d}) {}
};
Dual d0{1.f}; // OK.
Dual d1{1.f, 1.f}; // OK.
// Dual d2{1.f, 1.f, 1.f}; // Error. I want this.
Dual d2{1.f, {1.f, 1.f}}; // OK. I don't want this.
是否可以只使用一个构造函数?
这样Dual d2{1.f, 1.f, 1.f};也可以。
可能是这样的(无法编译):
struct Dual {
float v;
std::valarray<float> d;
Dual(float v, float d...): v(v), d({d...}) {}
};
Dual d0{1.f};
Dual d1{1.f, 1.f};
Dual d2{1.f, 1.f, 1.f}; // I want this.
我应该使用可变参数模板还是std::initilizer_list<>?
以及如何使用?
【问题讨论】:
-
您希望所有 4 个版本都使用单个构造函数,还是前 3 个版本足够?
-
前三个。我会在问题中澄清。
标签: c++ variadic-templates variadic-functions