Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a converting constructor with more than a single non-optional parameter look like and why does it make sense?

As far as I have used "converting constructors" they look like this:

struct X {
  X(A); // conversion from A -> X
  X(B,C = someC); // conversion from B -> X, with some default C
};
X x1 = A(); // calls X::X(A())
X x2 = B(); // calls X::X(B(),someC)

This makes perfect sense, and as far as I know works as long as you don't have a constructor:

struct Y {
  Y(A,B); // no implicit conversion
};

However, this is where it gets interesting, the C++11 standard reads literally:

12.3.1 Conversion by constructor

  1. A constructor declared without the function-specifier explicit that can be called with a single parameter specifies a conversion from the type of its first parameter types of its parameters to the type of its class. Such a constructor is called a converting constructor.

(italics were originally underlined, but markdown doesn't accept <u>)

This seems to suggest that it was changed that a converting constructor doesn't have to be callable "with a single parameter" and the change from "type of its first parameter" to "types of its parameters" (note the plural!) further supports this. While I would expect that "type of its first parameter" would be changed to "type of its single non-optional parameter" (1) or even "type of its parameter that received an explicit argument" (2) in order to allow these:

struct Z {
  Z(A = a, B, C = c); // (1)
  Z(D = d, E = e, F = f); // (2)
};
Z = D(); // (2)
Z = E(); // (2)

I don't see how the formulation with the plural form makes sense: Does it really indicate you can perform conversion with several arguments? What does this mean?

like image 300
bitmask Avatar asked Nov 20 '12 15:11

bitmask


People also ask

What is a converting constructor?

A conversion constructor is a single-parameter constructor that is declared without the function specifier explicit . The compiler uses conversion constructors to convert objects from the type of the first parameter to the type of the conversion constructor's class.

What is a default and conversion constructor in Java?

A conversion constructor is any non- explicit constructor callable with one argument. In your code sample, as the Complex constructor provides default values for its parameters, it can be called with a single argument (say 3.0 ).

Can a constructor have parameter for structure in C++?

Constructors can also take parameters (just like regular functions), which can be useful for setting initial values for attributes.

What is a conversion function in C++?

You can define a member function of a class, called a conversion function, that converts from the type of its class to another specified type.


1 Answers

The language was modified as part of the addition of initializer lists. See n2672: init-list wording.

An example:

struct S {
    S(int x, double y) { }
};

void f(S) { }

int main() {
    f({ 42, 42.0 });
}
like image 84
James McNellis Avatar answered Sep 22 '22 00:09

James McNellis