gpt4 book ai didi

c++ - 隐式转换和调用函数

转载 作者:行者123 更新时间:2023-11-28 01:58:44 25 4
gpt4 key购买 nike

当我调用一个将参数作为 int 的函数,同时传递一个 double 参数时,在 C++ 中隐式转换有什么用?为什么这不是编译时错误?允许这种情况发生的运行时堆栈中发生了什么?

#include <iostream>
void foo(int num);

int main() {
double doub= 4.9;
foo(doub);
return 0;
}

void foo(int num){
std::cout<<num<<std::endl;
}

最佳答案

这是所有 implicit conversions 的一长串 list C++ 做的。这个特定的包含在:

Floating–integral conversions A prvalue of floating-point type can be converted to a prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded. If the value cannot fit into the destination type, the behavior is undefined (even when the destination type is unsigned, modulo arithmetic does not apply). If the destination type is bool, this is a boolean conversion (see below).

关于c++ - 隐式转换和调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40314479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com