模板类在模板类型之间转换,但也专门化
Template classes conversion between template types but also specialised
基本上我想完成两种模板类型之间的转换
但我希望赋值运算符或复制构造函数是专业化的。
例如,我有一个颜色类别:
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)
{
}
};
通常,在0
和1
之间需要颜色分量作为float
s。然而,通常更容易将组件提供为0
和255
之间的数字,因为这通常是在Photoshop或GIMP中得到的。
因此,我希望这个类的实例能够在float
和int
类型之间转换:
color<int> c1(255,234,122,14);
color<float> c2 = c1;
当它这样做时,c1
中的数字除以255
,得到0
到1
的等价物。
到目前为止,我已经做了这件事:
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。我不知道如何专门化这个构造函数(或赋值运算符),使其仅对int
到float
专业有效。
编辑
也许这确实解决了你的问题。您只想完全专门化转换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&) { ... }
}
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 是否可以对零模板参数进行模板专门化
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 模板类在模板类型之间转换,但也专门化
- 是否有一种方法可以在编译时和运行时之间专门化一个函数