4

On cppreference about list-initialization in the second intend (for copy-list-initialization) it says:

copy-list-initialization (both explicit and non-explicit constructors are considered, but only non-explicit constructors may be called)

What exactly is the difference about constructors being 'considered' and being actually 'called'. Why consider constructors, that may not be called anyway?

Reizo
  • 1,374
  • 12
  • 17
  • 3
    I assume it means that explicit constructors participate in overload resolution as well, but if such constructor happends to be the best match, the program is ill-formed. – HolyBlackCat Oct 30 '18 at 09:47
  • I guess it means that overload resolution does not care if it's explicit until after the overload has been selected. – One Man Monkey Squad Oct 30 '18 at 09:47
  • 1
    "why are there different forms of list-initialisation at all?" - this is actually a good question (though it probably does not fit into SO format). C++ already has 11(?) forms of initialization for no particular reason (and probably more are coming in the newer standards). someone needs to curb standard committee. – user7860670 Oct 30 '18 at 09:51
  • 1
    This question is partly duplicate in here, you can find good info at least related to your last question. Please read: https://stackoverflow.com/questions/13461027/why-does-the-standard-differentiate-between-direct-list-initialization-and-copy – Jules Oct 30 '18 at 10:02
  • @Jules Thanks, removed the additional question. – Reizo Oct 30 '18 at 10:18

1 Answers1

4

The difference between "considered" and "called" is that "considered" means the candidate function participates in overload resolution while "called" means that it is actually chosen as the best match. Explicitly (pun not intended), this means that if an explicit constructor is selected during copy list initialization, it's forbidden. For example consider this scenario:

struct String {
  explicit String(int size);
  String(char const *value);
};

String s = { 0 };

Here, you are using an implicit conversion where the explicit constructor would be a better match so the compiler rightfully rejects it. You need to write String{ 0 } to fix the code. Now imagine if explicit constructors were not considered and the former was legal code. It would be very strange to have an explicit constructor that did nothing.

  • So, when explicit constructors _would be called_, the first constructor was legally called, and if explicit constructors _would not be considered_, the second constructor was legally called? What if the first constructor did not exists in the initial example, would it compile? – Reizo Oct 30 '18 at 10:27