2

Today I was surprised by this paragraph at cppreference:

Converting constructor

A constructor that is not declared with the specifier explicit and which can be called with a single parameter (until C++11) is called a converting constructor.

The "(until C++11)" bit seems to be referring to the phrase "which can be called with a single parameter".

I didn't know this changed at C++11. What's the change? What kind of "converting constructor" can take more than one argument? (If that's what it is referring to.)

(I suppose it could be referring to constructors that have more than one argument but those extra arguments - or all of them - have default values. But default values for arguments have been in C++ forever. So did something change there - before C++11 you couldn't implicitly convert with a constructor that took default arguments? But this example at wandbolt shows that that did work in C++03.)

davidbak
  • 5,775
  • 3
  • 34
  • 50
  • https://stackoverflow.com/a/15077788/11683? – GSerg Jun 28 '21 at 18:05
  • @GSerg - I gotta say, I could have sworn I typed "converting constructor" into the SO search bar and did not see that. Maybe I should delete this question or VTC as dup? – davidbak Jun 28 '21 at 18:09
  • 1
    Not deleting [is much better](https://meta.stackoverflow.com/a/265737/11683)! – GSerg Jun 28 '21 at 18:11
  • @GSerg - ok! Entered a self-VTC. (You know, there should be a badge for that ...) – davidbak Jun 28 '21 at 18:14
  • 1
    That's strange; did you simply cast a close vote, or did you accept the "this is a duplicate" suggestion once GSerg cast a vote? If it was the latter, the question should have been closed immediately, I think. (No badge either way, though ;)). – cigien Jun 28 '21 at 18:30
  • @cigen - I did a VTC specifying duplicate. I didn't see a prompt asking me to accept a duplicate suggestion ... no banner or anything ... – davidbak Jun 28 '21 at 18:43
  • Hour long presentation [The Nightmare of Initialization in C++](https://www.youtube.com/watch?v=7DTlWPgX6zs) by Nicolai Josuttis at CppCon 2018. – Eljay Jun 28 '21 at 18:44

1 Answers1

1

An example with two parameters is given later in the page you cited:

A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Huh. I didn't read that far. I didn't think that would be called a "converting constructor" though. I would have thought it was called "copy list initialization" ... because, what type is it converting _from_? – davidbak Jun 28 '21 at 18:08
  • It's converting from a braced initialization list. – Sam Varshavchik Jun 28 '21 at 18:08
  • So the type is "std::initializer_list"? Ok I guess. – davidbak Jun 28 '21 at 18:10
  • No, it's not a `std::initializer_list`. `{4, "foobar"}` could be a braced initialization list, but good luck figuring out a single template parameter that produces something like this. Braced initialization lists are actually typeless. – Sam Varshavchik Jun 28 '21 at 18:12
  • But if they're _typeless_ isn't it an abuse of terminology to call it a use of a "converting constructor" in that example? (Not a serious question, really, I learned long ago not to be bothered by that kind of thing ... just accept it and move on ...) – davidbak Jun 28 '21 at 18:13
  • @davidbak "Unlike explicit constructors, which are only considered during direct initialization (which includes explicit conversions such as static_cast), converting constructors are also considered during copy initialization, as part of user-defined conversion sequence." the term "converting constructor" is just a term describing the same as that paragraph in two words – 463035818_is_not_an_ai Jun 28 '21 at 18:30
  • @463035818_is_not_a_number - ok, but I wouldn't have considered braced-list-initialization as part of a "user-defined conversion sequence" which to me (not the standards people) means, you know, _user-defined_ conversions! Conversion operators you write, or constructors which can be called with a single argument ... But I agree with you: "it's just a term". You can't pay too much attention to the English meaning of the words ... – davidbak Jun 28 '21 at 18:46