|
|
 |
 |
 |
 |
Copy construction with inaccessible base class copy c-tor
Hello, Take a look at this program: ----------------------------------- class B { B(const B&); B& operator=(const B&); public: B(int); };
class C : public B { public: C(int); };
int main() { C c = C(42); // Comeau: no error B b = B(42); // Comeau: error }
----------------------------------- Both 'c' and 'b' are copy-constructed, as I understand it. B's copy c-tor is inaccessible, so construction of 'b' is ill-formed. And Comeau (online test drive) flags it such. But it lets the construction of 'c' through. Should it? As I understand it, a temporary can be omitted, but its creation should be possible (12.2/1) as if it weren't omitted. And since C's copy c-tor cannot be created (12.8/7), the code that requires (or would require) it is also ill-formed. Where do I err? Or do I? Thanks! V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
Hello, On Feb 26, 9:35 pm, "Victor Bazarov" <v.Abaza@comAcast.net> wrote:
> Hello, > Take a look at this program: > ----------------------------------- > class B > { > B(const B&); > B& operator=(const B&); > public: > B(int); > }; > class C : public B > { > public: > C(int); > }; > int main() > { > C c = C(42); // Comeau: no error > B b = B(42); // Comeau: error > } > ----------------------------------- > Both 'c' and 'b' are copy-constructed, as I understand it.
If everything were right the copy constructor would be called. > B's copy c-tor is inaccessible, so construction of 'b' is > ill-formed. And Comeau (online test drive) flags it such.
yes. b cannot be copy constructed. > But it lets the construction of 'c' through. Should it?
NO. c cannot be copy constructed as well. Compiler shall not generate a copy constructor as the base class copy constructor is private. SO this is an error too. > As I understand it, a temporary can be omitted, but its > creation should be possible (12.2/1) as if it weren't omitted. > And since C's copy c-tor cannot be created (12.8/7), the code > that requires (or would require) it is also ill-formed. > Where do I err? Or do I? > Thanks! > V > -- > Please remove capital 'A's when replying by e-mail > I do not respond to top-posted replies, please don't ask
-- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
Victor Bazarov wrote: > Hello, > Take a look at this program: > ----------------------------------- > class B > { > B(const B&); > B& operator=(const B&); > public: > B(int); > }; > class C : public B > { > public: > C(int); > }; > int main() > { > C c = C(42); // Comeau: no error > B b = B(42); // Comeau: error > } > ----------------------------------- > Both 'c' and 'b' are copy-constructed, as I understand it. > B's copy c-tor is inaccessible, so construction of 'b' is > ill-formed. And Comeau (online test drive) flags it such. > But it lets the construction of 'c' through. Should it?
Sounds logical. gcc rejects both lines, Sun CC only barfs on C c = C(42); -- Ian Collins. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
 |
 |
 |
 |
|