简体   繁体   English

类模板仅适用于2种类型

[英]Usefulness of a Class Template for 2 types only

编写仅对2种类型有意义的类模板(即floatdouble )有什么优点(除了可维护性)?

Maintainability is the one single advantage, you want to keep your code DRY (Don't Repeat Yourself) without duplicated code. 可维护性是唯一的优势,您希望在没有重复代码的情况下保持代码干燥(不要重复自己)。

I don't see any problems with a template for just two types - float and double are similiar, but there are no other types that behave in the same way. 我没有看到只有两种类型的模板有任何问题 - floatdouble数字类似,但没有其他类型的行为方式相同。

Because it may be applicable to use std::complex<float> as a type? 因为它可能适用于使用std::complex<float>作为类型? It the template is really generic to a floating point (or integer) type, who said that there may not come additional larger types along the road? 模板对于浮点(或整数)类型来说是非常通用的,他说在路上可能没有更多更大的类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM