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));
Is foo(1) constructor call or function-style cast?
They are the same thing.
<!---------> (I had to see how you managed that) +1: that was at @OliCarlesworth :)It's a function-style cast that results in a constructor call, so both.
foo(1) part.5.2.3 Explicit type conversion (functional notation)
1 A simple-type-specifier (7.1.6.2) or typename-specifier (14.6) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). ...
Your code creates a temporary, using the constructor you have with the argument's value 1, and binds it to a const reference. The temporary's lifetime ends at the end of the statement where it was created.
newas an expression, not as a variable declaration statement. (That said it's more of a vague guess which is why it's a comment not an answer.)