int( 5 ) - cast or constructor?

Like the title says, if I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
 
void f( int num )
{
    std::cout << num << std::endl;
}
 
int main()
{
    f( int( 5 ) );
    
    return ( 0 );
}


Does line 10 cast literal 5 to an integer, stores the result in a temporary int and pass that one to f(), or creates it a temporary integer using the int ctor and passes that one?
Last edited on
int(5) is, by definition, the same as (int)5, which is the same as static_cast<int>(5).

There is no "int constructor" for the static cast to call, and the type of 5 is already int, so it does nothing ( theoretically, it builds a new prvalue temporary of type int, with value 5)

see http://en.cppreference.com/w/cpp/language/explicit_cast
Last edited on
function style explicit type casting an int to int.

Aceix.
Ok, thanks. So there is no int constructor at all? I always thought these examples:
1
2
3
4
5
6
int a( 5 );

int f()
{
    return ( int() ); // returns 0
}

use the int ctor.

EDIT: apparently not, according to this: http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors

Anyways thanks for the help :)
Last edited on
Topic archived. No new replies allowed.