专门化模板化的内部模板 class

Specializing inner template of templated class

所以我有一个模板图像 class,我正在尝试为其设置内联颜色转换。这是让我烦恼的代码的简化:

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;
    }

};

…问题是模板化转换运算符后面的模板特化不能在class级别定义(根据我得到的错误)。

我知道需要离线指定专业化,但我一辈子都搞不懂语法。我应该如何申报该专业?

只需删除专业化。如果 DestColorColor 相同,您的转换函数将永远不会被调用。 [class.conv.fct]/p1:

A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to (possibly cv-qualified) void.