模板中的浮动广告转换 [英] float conversions in templates

查看:94
本文介绍了模板中的浮动广告转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此函数模板:

template <typename T>
T divby2(T a)
{
  return .5 * a;
}

是否有一种方法可以指定( .5 ), double 常量,以便在运行时不会转换为 T T!= double (比如,当 T float )?

Does there exist a way to specify (.5), a double constant, so that it won't be converted at run time into T when T != double (say, when T is float)?

.5常量的替代规范的一些想法:

Some ideas for alternative specifications of the .5 constant:

template <typename T>
T divby2(T a)
{
  return T(.5) * a;
}

template <typename T>
T divby2(T a)
{
  return (T(1) / T(2)) * a;
}


推荐答案

转换。将值转换为另一种类型的决定是在compiletime。

There are no decisions made about conversions at runtime. The decision to convert a value to another type or not is made at compiletime.


  • .5 $ 将会是您拥有的任何模板专业化,即 T
  • 乘法的结果类型(让我们称为 X T)给出。对于所有的内置数字,除了long double,它给出一个长double

  • 因为你返回一个 T 乘法返回的 X 将转换为 T

  • T(0.5)将始终为T.

  • .5 will be a double, no matter what.
  • a will be whatever template specialization you have, i.e. of type T
  • The result type of the multiplication (let's call it X) will be whatever operator*(double, T) gives. A double for all the builtin numbers, except for long double, where it gives a long double
  • Since you are returning a T, the X returned by the multiplication will be converted into a T
  • And T(0.5) will be always a T.

转换或运算符* 未定义,您会收到编译时错误。类型与运行时无关(除非你有虚拟函数等)。

If any of those conversions or the operator* are not defined, you get a compile time error. Types have nothing to do with runtime (unless you have virtual functions and the like).

给你的评论: T(.5) code>是类型T的表达式。从概念上来说,的转换在运行时发生。然而,允许编译器优化,例如,如果T是int,编译器将用 int(.5)实例化 T(.5)优化到 0

To your comment: T(.5) is an expression of type T. The conversion of the values will conceptually take place at runtime. However, the compiler is allowed to optimize that away, e.g. if T is int, the compiler would instantiate the T(.5) with int(.5) and could immediately optimize that to 0.

从你的问题,我假设你可能不知道模板的性质。模板在 compiletime 进行评估和实例化,与其他一些语言的通用函数不同。模板实例化意味着,编译器为您使用模板的每个类型生成独立函数。例如如果你使用 T = double T = int T = long double 在不同的地方,就好像你写了三个函数:

From your questions I assume that you might not be aware of the nature of templates. Templates are evaluated and instantiated at compiletime, unlike generic functions in some other languages. Template instantiation means, that the compiler generates independent functions for each type you use the template with. So e.g. if you use that function with T=double, T=int and T=long double in different places it would be as if you had written three functions:

double divby2(double a)
{
  return .5 * a;
}

int divby2(int a)
{
  return .5 * a;
}

long double divby2(long double a)
{
  return .5 * a;
}

在第一个函数中,根本不会发生转换,双。在第二个函数中,编译器知道double乘以int给出一个double,但是double被转换回int。你可能会得到警告。在第三个函数中,double和long double的乘积给出了long double。因为返回类型是一个长双,以及一切都很好,你不会得到警告。

In the first function, no conversions will take place at all, because everything is double. In the second function, the compiler knows that double multiplied by an int gives a double, but that double is converted back to int. You might get warnings about that. In the third function, the multiplication of a double and a long double gives a long double. Since the return type is a long double as well, everything is fine and you won't get warnings.

这篇关于模板中的浮动广告转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆