【发布时间】:2017-02-11 23:13:16
【问题描述】:
我正在研究一个类来识别纹理中的图像,如下所示:
using level_t = ...
using layer_t = ...
using face_t = ...
template <bool>
struct levels {};
template <>
struct levels<true> {
level_t level = level_t{0};
};
template <bool>
struct layers {};
template <>
struct layers<true> {
layer_t layer = layer_t{0};
};
template <bool>
struct faces {};
template <>
struct faces<true> {
face_t face = face_t{0};
};
template <bool HasLevels, bool HasLayers, bool HasFaces>
struct index : levels<HasLevels>, layers<HasLayers>, faces<HasFaces> {};
level_t、layer_t 和 face_t 是 int 的一种“强类型定义”(基本上是不同的类型,看起来像 int 但没有隐式转换)。
现在我可以像这样使用我的类型了:
using Index1 = index<true, true, false>;
void f(Index1);
Index1 v;
v.level = level_t{1};
v.layer = layer_t[1};
f(v);
但为了让我的用户的生活更轻松,我想允许此代码:
f({level_t{1}, layer_t{1}};
由于基类,聚合初始化已经失效,所以我需要自己编写一个构造函数;编写支持所有参数组合(但不支持重新排序)的构造函数的最佳/最智能方法是什么?
【问题讨论】:
-
是否有理由必须使用多重继承而不是聚合?
-
all the argument combinations (but not the reorder)是什么意思?您是否在没有重新排序构造函数参数的情况下引用任何模板参数值? -
@zneak 在这种情况下是的,根据
index类型的“配置”方式,某些属性必须消失,尝试使用它们必须导致编译错误 -
@PawełStankowski given
index<true, false, true>我想要一个接受 (level_t, face_t) 但不接受 (face_t, level_t) 的构造函数