Reputation: 25
I found there is an interesting syntax in C++ function declaration and its defination. for example,
bool myLess(const int& x=int(),const int& y=int());
how to understand the "=int()" in this declaration?
I guess its effect is to transform the input actual arguments of other types into int type, it is surely so. I can call this function as
double x1{3.5}, x2{4.5};
bool b = myLess(x1,x2);
but I cannot understand this syntax, can anyone give me some answer? where can i find this syntax in ISO C++ standard?
Upvotes: 0
Views: 319
Reputation: 7905
You had asked:
how to understand the "=int()" in this declaration?
bool myLess(const int& x=int(),const int& y=int());
Your function declaration of course returns a bool, but it accepts two parameters which are both a const reference of an integer type. The =
for each parameter is giving the parameters a default value. The int()
is calling the basic type's default constructor. The default constructor for int
will end up being a prvalue with a value of 0
. This will then cause both x
and y
to be assigned to or initially constructed with a value of 0
. Then whatever is passed into the function as arguments will overwrite this initial value.
You may see function declarations written like this just to ensure that the parameters themselves are always initialized to something specifically 0
. This way if the function is ever called and the references are used after the function call, the next calculation that uses the referenced variables will not use garbage data, especially if the function's internal code has checks for valid data and does something different if the value is 0
. This will ensure that if only 1 parameter was passed, at least the defaulted is constructed to 0
.
It has no different effect than:
bool myLess(const int& x=0, const int& y=0);
When it comes to your used example:
double x1{3.5}, x2{4.5}; bool b = myLess(x1,x2);
When you are passing x1
and x2
into this function, what happens here is the double
type is being casted to an int
type and you will lose data or information due to truncation. Even though x1
has a value of 3.5
and x2
has a value of 4.5
, if your compiler will compile the above code, the values that the function will see are 3
and 4
respectively.
Upvotes: 0
Reputation: 386
The declaration simply gives default arguments and has the same effect as
bool myLess(const int& x = 0, const int& y = 0);
You write:
I guess its effect is to transform the input actual arguments of other types into int type, it is surely so.
That is not an effect of the int()
! Type casting from double
s is done anyways, and would work also if you use = 0
. More on int()
is also said in the question what does int() do in C++?.
You also asked where to find this in Iso C++. Here is a relevant passage (from the N3690 draft):
[§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.
And in 8.5 we see that int
s are value-initialized by being zero-initialized.
Upvotes: 1