如何将浮点值传递给以双精度作为参考值的函数

发布于 2025-01-08 09:10:51 字数 351 浏览 0 评论 0原文

我有一个函数,其原型如下所示:

void example (double &var);

但是,我的问题是我可能还需要使用一些浮点值调用函数。 例如,

float temp=10.1;

example(temp)

如果我这样做,我的代码将无法编译,可能是因为将浮点值传递给双引用变量。

我想避免为 double 和 float 编写重载函数。

有人可以建议一个更干净/更好的方法来实现这个吗?

函数基本上是一个截断函数,它截断给定的输入 &是的,原来的已经修改了。

谢谢。

I have a function whose prototype looks like this:

void example (double &var);

But, my problem is I might require to call function with some float values as well.
e.g.

float temp=10.1;

example(temp)

If I do this my code don't compile, probably because of passing float value to double reference variable.

I want to avoid writing the overloaded function for double and float.

Can someone please suggest a cleaner/better way to implement this?

Function is basically a truncate function that truncate the input given & yes the original is modified.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

闻呓 2025-01-15 09:10:51

模板函数怎么样?

template <typename T>
void example (T &var);

编译器将替换 floatdouble 用法(请记住,C++ 模板是具有类型安全的宏)

How about a template function?

template <typename T>
void example (T &var);

The compiler will replace both float and double usages (keep in mind C++ templates are macros with type-safety)

你是暖光i 2025-01-15 09:10:51

不建议通过引用传递小类型。你最好有类似 double example(double var); 的东西。这也将解决你的问题。

之前:

void example(double& d);

double d = ...;
example(d); // unclear if d was modified

之后:

double example(double d);

double d = ...;
d = example(d);

float f = ...;
f = static_cast<float>( example(f) ); // cast required to tell the compiler to allow
                                       // the precision loss

Passing small types by reference is not recommended. You'd better have something like double example(double var);. That will solve your problem as well.

before:

void example(double& d);

double d = ...;
example(d); // unclear if d was modified

after:

double example(double d);

double d = ...;
d = example(d);

float f = ...;
f = static_cast<float>( example(f) ); // cast required to tell the compiler to allow
                                       // the precision loss
残月升风 2025-01-15 09:10:51

您可以将其模板化。

template<typename T>
void example (T &var)

You can template it.

template<typename T>
void example (T &var)
浅笑依然 2025-01-15 09:10:51

解决方案取决于 example 到底在做什么。

  • 它是否改变了通过引用传递的值?如果是,则无法将 float 传递给它。通过强制转换会编译,但会在运行时崩溃(因为您要传递 4 个字节,而需要 8 个字节)。
  • 如果它不改变值,您只需向其添加 const 属性,并传递任何基本数据类型。它将是:void example (const double &var);。如果可以进行转换(因为它现在被视为 (double)),编译器就不会抱怨。
  • 使其成为函数模板可能会也可能不会起作用,这完全取决于函数正在做什么。

Solution depends on what exactly example is doing.

  • Is it changing the value passed by reference? If yes, you cannot pass float to it. Passing by forceful conversion will compile, but would crash at runtime (since you'd be passing 4 bytes, where 8 bytes were needed).
  • If it is not changing value, you can just add const attribute to it, and pass any basic datatype. It would be: void example (const double &var);. If conversion is possible (since it is now treated as (double)), compiler wouldn't complain.
  • Making it function template may or may not work, and it entirely depends on what function is doing.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文