Bug 55437 - Non-const copy constructor causes error - even if not called
Summary: Non-const copy constructor causes error - even if not called
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.5
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-11-22 05:17 UTC by Rocky Downs
Modified: 2012-11-22 12:48 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rocky Downs 2012-11-22 05:17:31 UTC
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&)
Comment 1 Jonathan Wakely 2012-11-22 12:48:09 UTC
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.