If I have a class A
, and I write A(5);
, it clearly makes a temporary variable.
But what is not clear if A(5);
is a constructor call (using 5
as parameter), or if this is a function style cast, casting 5
to A
. Can someone explain it to me please?
2) The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses. This cast is exactly equivalent to the corresponding C-style cast expression.
A constructor for a class type is called whenever a new instance of that type is created. If a cast creates a new object of that class type then a constructor is called.
The C++ cast operators are keywords defined in the language. While they look like template functions, they are part of the language itself, i.e.the behavior is implemented in the compiler, not in the standard library.
When you use static_cast , by defaut (i.e. without optimizations activated) it calls the conversion constructor of the object you are trying to cast into (if it exists). For instance, in this code. The highlighted expression would call the following constructor (if existent): Foo(const Bar &) .
It's a functional-style type conversion which creates a t
from an int
by calling the constructor. There is no way to explicitly call a constructor in C++.
This is described in [expr.type.conv]
(N3337):
5.2.3 Explicit type conversion (functional notation)
1) A simple-type-specifer (7.1.6.2) or typename-specifer (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). If the type specified is a class type, the class type shall be complete. If the expression list specifies more than a single value, the type shall be a class with a suitably declared constructor (8.5, 12.1), and the expression
T(x1, x2, ...)
is equivalent in effect to the declarationT t(x1, x2, ...);
for some invented temporary variablet
, with the result being the value oft
as a prvalue.
Since t
is a simple-type-specifier, this is equivalent to the corresponding cast expression. This is allowed to carry out the equivalent of a static_cast
([expr.cast]/4
), which defines the final result of the conversion:
[expr.static.cast]/4:
Otherwise, an expressione
can be explicitly converted to a typeT
using astatic_cast
of the formstatic_cast<T>(e)
if the declarationT t(e);
is well-formed, for some invented temporary variablet
(8.5). The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The expressione
is used as a glvalue if and only if the initialization uses it as a glvalue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With