In your case of a suitable copy constructor, this is not your reason, because the first parameter of your copy constructor is declared as a non-constant lvalue reference. This link cannot be linked to the temporary Koords{} object Koords{} . Therefore, up to C ++ 17 (see below), this cannot work in principle, and explicit nothing to do with it.
As for explicit on the copy constructor ... The copy constructor by definition has always been a special case of the conversion constructor. Declaring a copy constructor as an explicit produces the same effect on it as on any other single-argument constructor. Such an explicit constructor will be used only in contexts corresponding to direct initialization (direct-initialization), and will not be used in contexts corresponding to copy initialization (copy-initialization)
struct S { S() {} explicit S(const S&) {} }; int main() { S s; S a(s); // OK S b = s; // Ошибка }
Returning a value from a function through return is done according to the rules of copy initialization . Therefore, the explicit copy constructor cannot be used there.
S foo() { S s; return s; // Ошибка }
Note that in C ++ 14 such options are erroneous for the same reason.
return S(s); return S(); return S{};
but starting with C ++ 17, these options are already correct because of guaranteed copy elision.
Interestingly, even in C ++ 14 the correct one is
S foo() { return {}; }
But I do not remember offhand what “corner” of the standard makes it possible.
fun, you use the default constructor, but its presence is not specified in the code. Those. The example is not complete. An ideal example when the code can be just ctrl + c, ctrl + v and look at the result. Without thinking anything out and pasting pieces from different parts. - αλεχολυτKoords()- everything. - Andrej Levkovitch