I believe the error below should not occur. The copy constructor in question is not exercised. Moreover, making the copy constructor const also makes the error not occur. class String{ public: String(String& s){} // No error if this line is removed String(const char* s){} }; int main(){ String S = (char*)"Test"; return 0; } test2.cpp: In function ‘int main()’: test2.cpp:9: error: no matching function for call to ‘String::String(String)’ test2.cpp:5: note: candidates are: String::String(const char*) test2.cpp:4: note: String::String(String&)
The program is ill-formed. String S = (char*)"Test"; is equivalent to String S = String((char*)"Test"); which requires an accessible copy constructor (or in C++11 move constructor) that takes an rvalue argument (i.e. temporary). A non-const reference cannot bind to a temporary, so your copy constructor is not viable. The copy constructor must be accessible and viable even if the copy is elided and the copy constructor is not used. You'll get the same error from any conforming C++ compiler.