gpt4 book ai didi

C++中的C++函数声明,类型转换

转载 作者:行者123 更新时间:2023-11-28 04:25:10 24 4
gpt4 key购买 nike

我发现在 C++ 函数声明及其定义中有一个有趣的语法。例如,

bool myLess(const int& x=int(),const int& y=int());

如何理解这个声明中的“=int()”?

我猜它的作用是将其他类型的输入实参转化为int类型,确实如此。我可以将此函数称为

          double x1{3.5}, x2{4.5};
bool b = myLess(x1,x2);

但是我不明白这个语法,谁能给我一些答案?我在哪里可以找到 ISO C++ 标准中的这种语法?

最佳答案

声明只是给出默认参数,效果与

相同
bool myLess(const int& x = 0, const int& y = 0);

你写:

I guess its effect is to transform the input actual arguments of other types into int type, it is surely so.

不是 int() 的效果! double 的类型转换无论如何都会完成,如果您使用 = 0 也可以工作。关于 int() 的更多信息也在问题 what does int() do in C++? 中提到。 .

您还询问了在 Iso C++ 中哪里可以找到它。这是一段相关的段落(来自 N3690 草案):

[§5.2.3(2)] The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, whose value is that produced by value-initializing (8.5) an object of type T; no initialization is done for the void() case.

在 8.5 中我们看到 int 是通过零初始化来进行值初始化的。

关于C++中的C++函数声明,类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493004/

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