【发布时间】:2015-11-14 17:54:09
【问题描述】:
所以我有一个模板化的 Image 类,我正在尝试为其设置内联颜色转换。这是让我烦恼的代码的简化:
template <typename Color = colors::RGB>
class Image {
/// ...
template <typename DestColor>
operator Image<DestColor>() {
/// when assigning with a different colorspace:
/// ... do the color conversion
/// ... and return a fresh Image<DestColor>
}
template <>
operator Image<Color>() {
/// when assigning with the same colorspace:
return *this;
}
};
...问题是模板化转换运算符后面的模板特化不能在类级别定义(根据我得到的错误)。
我知道需要在行外指定专业化,但我终其一生都无法弄清楚语法。我应该如何声明该专业化?
【问题讨论】:
-
如果我没记错的话,你不能在一个类中声明一个完整的模板类特化,添加一个虚拟类模板参数 default 为 void 可能会解决问题,如果它不打扰你。
-
啊哈,如果我可以问一下,那会是什么样子?
-
它看起来像that (编辑:您也可以将其设为私有并声明一个公共别名/模板派生结构以隐藏该虚拟参数,如果它困扰您并且您没有更好的方法)
-
有趣,如果这与
operator声明一起使用,它可能是解决方案。有没有关于隐藏参数的私有别名语法和方法的例子? -
我重读了这个问题,并注意到我误读了内部类声明的转换运算符,在这种情况下,只需重载转换运算符就可以了example(当然,您可以应用上述技巧,但我想到的是内部类而不是方法)
标签: c++ templates inline template-specialization partial-specialization