class K {
public:
int x_;
explicit K(int x) : x_{x} {}
explicit K(const K& k) : x_{k.x_} {} // No error is seen if this is made
}; // non explicit.
K factory_function(int x) {
return K{x};
}
int main() {
K k1{factory_function(20)}; // [1] works fine with explicit copy constructor.
K k2 = factory_function(30); // [2] gives error with explicit copy constructor.
return 0;
}
r1.cc: In function ‘K factory_function(int)’:
r1.cc:54:13: error: no matching function for call to ‘K::K(K)’
return K{x};
^
r1.cc: In function ‘int main()’:
r1.cc:59:29: error: no matching function for call to ‘K::K(K)’
K k2 = factory_function(30);
factory_function() returns an object of type "K" ==> Does [2] still requires conversion ?
It'll be really helpful if I can get a detailed answer on where copy/conversion is needed and why it works if we make copy constructor non explicit.