简体   繁体   English

在模板中定义常量值的最佳方法

[英]Best way to define constant value in template

When I implement vector3 class, I have a simple question.当我实现vector3类时,我有一个简单的问题。

There are several ways to define a constant value in a template.有多种方法可以在模板中定义常量值。 What are these differences?这些区别是什么? And which one should I use?我应该使用哪一种?

template<typename T> ; template<typename T> ;

1: T{1} 1: T{1}

2: T{1.0f} 2: T{1.0f}

3: T{1.0} 3: T{1.0}

4: static_cast<T>(1.0) 4: static_cast<T>(1.0)

None of those make a difference, unless you have a type that converts from int differently than from float.这些都没有区别,除非您的类型从 int 转换为与从 float 不同。 I prefer the first, as it is the briefest我更喜欢第一个,因为它是最短的

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

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