模板类在模板类型之间转换,但也专门化

Template classes conversion between template types but also specialised

本文关键字:专门化 之间 类型 转换      更新时间:2023-10-16

基本上我想完成两种模板类型之间的转换

但我希望赋值运算符或复制构造函数是专业化的。

例如,我有一个颜色类别:

template<typename T = float>
class color
{
public:
    T r;
    T g;
    T b;
    T a;
    color(T R, T G, T B, T A)
    : r(R), g(G), b(B), a(A)
    {
    }
};

通常,在01之间需要颜色分量作为floats。然而,通常更容易将组件提供为0255之间的数字,因为这通常是在Photoshop或GIMP中得到的。

因此,我希望这个类的实例能够在floatint类型之间转换:

color<int> c1(255,234,122,14);
color<float> c2 = c1;

当它这样做时,c1中的数字除以255,得到01的等价物。

到目前为止,我已经做了这件事:

template<typename U>
color<T>(color<U> c)
: r(c.r/255.0), g(c.g/255.0), b(c.b/255.0), a(c.a/255.0)
{
}

但这也会将float实例除以255。我不知道如何专门化这个构造函数(或赋值运算符),使其仅对intfloat专业有效。

编辑

也许这确实解决了你的问题。您只想完全专门化转换color<int>->color<float>的构造函数,反之亦然。这是允许的。

#include <iostream>
using namespace std;
template<typename T>
class color
{
public:
    T r,g,b,a;
    color(T r, T g, T b, T a) : r(r), g(g), b(b), a(a) {}
    template<typename OtherT>
    color(const color<OtherT>&);
};
template<>
template<>
color<int>::color(const color<float>& other) : 
r(other.r * 255), 
g(other.g * 255), 
b(other.b * 255),
a(other.a * 255)
{}
int main() {
    color<float> c1 = { 1.0f, 1.0f, 1.0f, 1.0f };
    color<int> c2 = c1;
    cout << c2.r << " " << c2.g << " " << c2.b << " " << c2.a << endl;
    return 0;
}

不过,我想我更喜欢我以前的答案,因为如果用户输入int或float以外的模板参数,这将产生难以解释的错误。另一种方法非常明确。

旧答案

您想要的主要问题是,您不能部分地专门化类模板中的单个方法。

如果模板类color只使用int和float这两个参数,我会这样安排:有一个包含公共代码的基模板类,以及从中派生并提供专用构造函数的两个类。

template<typename T> class base_color { ... common code between int and float };

然后是两个具有特定转换构造函数的类

class int_color : public base_color<int>
{
public:
    int_color(const float_color&) { ... }
}
class float_color : public base_color<float>
{
public:
    float_color(const int_color&) { ... }
}