Constructor call or function-style in C ++

If I have the following C ++ code:

class foo{ public: explicit foo(int i){}; }; void f(const foo &o){ } 

And then I call

 f(foo(1)); 

Constructor call foo(1) or function-style?

+4
source share
3 answers

5.2.3 Explicit type conversion (functional notation)

1 A simple type specifier (7.1.6.2) or a typename-specifier (14.6) and then a list of expressions in parentheses builds a value; the specified type defines a list of expressions. If the list of expressions is a single expression, the type conversion expression is equivalent (in definiteness, and if defined in meaning) to the corresponding litas expression (5.4) ....

Your code creates a temporary one using the constructor that you have with the value 1 argument and associates it with a reference to const. The temporary time of life ends at the end of the instruction where it was created.

+5
source

It is the same.

+4
source

This is a function-style cast that leads to a constructor call, therefore both.

+4
source

Source: https://habr.com/ru/post/1383352/


All Articles