Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function style casting vs calling constructor

Tags:

c++

casting

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?

like image 484
andre3312 Avatar asked May 16 '16 13:05

andre3312


People also ask

What is function style cast?

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.

Does casting call the constructor?

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.

What is a function style cast in C++?

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.

Does static cast use constructor?

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 &) .


1 Answers

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 declaration T t(x1, x2, ...); for some invented temporary variable t, with the result being the value of t 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 expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t (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 expression e is used as a glvalue if and only if the initialization uses it as a glvalue.

like image 61
TartanLlama Avatar answered Oct 11 '22 22:10

TartanLlama