Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy construction with inaccessible base class copy c-tor

Reply
Thread Tools

Copy construction with inaccessible base class copy c-tor

 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2007
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! ]

 
Reply With Quote
 
 
 
 
Arun
Guest
Posts: n/a
 
      02-27-2007
Hello,

On Feb 26, 9:35 pm, "Victor Bazarov" <(E-Mail Removed)> 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! ]

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-27-2007
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! ]

 
Reply With Quote
 
Nicola Musatti
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 3:35 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
[...]
> 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
> }

[...]
> 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?


I believe you're right. To me 12.8-7 appears quite clear on the
subject: The copy constructor is defined even if it's use is elided
and as it has an inaccessible base class copy constructor, the program
is ill-formed.

Cheers,
Nicola Musatti


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
Craig Scott
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 1:35 pm, "Victor Bazarov" <(E-Mail Removed)> 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?
>
> 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?


To my reading, section 12.8.7 (which you mention) is very clear on
this point:

"A program is illformed if the class for which a copy constructor is
implicitly defined has ... a base class with an inaccessible or
ambiguous copy constructor."

This is exactly your situation, since C has an implicit copy
constructor and the copy constructor in its base, B, is inaccessible.
If there is any confusion about whether the copy constructor or the
assignment operator is being used, the assignment operator is subject
to the same restriction (see 12.8.12) and would make the example ill
formed for the same reasons. Thus, any discussion about whether the
compiler is allowed to by-pass the assignment operator and use the
copy constructor instead should reach the same conclusion. For
reference, 12.8.15 discusses how the compiler is allowed to by-pass
the assignment and directly copy-construct in-place, but it should be
noted that it does not *require* an implementation to do that.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-28-2007
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?
>
> 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?


You are quite correct. I think this is a bug in Comeau. If I am not
mistaken, a similar (maybe the same) bug already appeared about a year ago
on this list:

http://groups.google.com/group/comp....dba796ee5478f5


Best

Kai-Uwe Bux

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      03-01-2007
On Feb 27, 3:35 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> 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
> }


The compiler should generate an error because you haven't provided
a body for the constructor of C. I'm assuming that either the
compiler stopped when it got the error on the B line, or it did
generate the error but you failed to report it.

AFAIK the C++ standard only says that compilers must generate a
diagnostic for particular erroneous code, and you reported that
a diagnostic was generated, so the behaviour is conforming. The
standard doesn't require that multiple diagnostics be produced,
even if there are multiple errors.

Try adding in bodies for all functions, and taking out the 'B' line
and see if you get an error. (If there is still no error, it would
be interesting to add further code to see how the compiler is
constructing the B part of the C object).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 8:55 am, "Old Wolf" <(E-Mail Removed)> wrote:
> On Feb 27, 3:35 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:


> > 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
> > }


> The compiler should generate an error because you haven't provided
> a body for the constructor of C.


That's undefined behavior, not an error requiring a diagnostic.
Most implementations will only detect it when linking, and of
course, he never got that far.

[...]
> Try adding in bodies for all functions, and taking out the 'B' line
> and see if you get an error. (If there is still no error, it would
> be interesting to add further code to see how the compiler is
> constructing the B part of the C object).


The error is, of course, that an accessible copy constructor is
required, but that the compiler cannot generate one for C,
because there isn't one for B. There is no requirement that the
copy constructor actually be called, however, and I know of no
compiler that will call it.

What is doubtlessly occuring is that the Comeau compiler sees
that it can generate the declaration for the copy constructor
for C, and doesn't go any further, since it doesn't actually
need the definition. (The standard makes it clear, however,
that it is a diagnosible error if the compiler is unable to
generate the definition, even if it doesn't use it.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
=?iso-8859-1?q?Daniel_Kr=FCgler?=
Guest
Posts: n/a
 
      03-01-2007
On 1 Mrz., 13:59, "James Kanze" <(E-Mail Removed)> wrote:
> That's undefined behavior, not an error requiring a diagnostic.
> Most implementations will only detect it when linking, and of
> course, he never got that far.


That's an interesting position, I never realized that. Does that mean,
that essentially every C++ program more or less relies on this kind
of UB? Even the standard provides class definitions, where
some of the usual suspects (copy c'tor, assignment op)
are declared but not defined, e.g. (I take N2134 as reference):

20.5.14.2:
// 20.5.14.2.6, undefined operators:
template<class Function2> bool operator==(const function<Function2>&);
template<class Function2> bool operator!=(const function<Function2>&);

22.1.1.1.2:
facet(const facet&); // not defined
void operator=(const facet&); // not defined

22.1.1.1.3:
void operator=(const id&); // not defined
id(const id&); // not defined

27.4.4:
basic_ios(const basic_ios& ); // not defined
basic_ios& operator=(const basic_ios&); // not defined

27.6.1.1.2:
sentry(const sentry&); // not defined
sentry& operator=(const sentry&); // not defined

27.6.2.3:
sentry(const sentry&); // not defined
sentry& operator=(const sentry&); // not defined

Of course one can argue that the standard is allowed to demand
that and library writers have to ensure it's realization, but that
seems
somewhat unsatisfactory to me.

Is this one of the implicite rules or explicitely spoken out?
(I haven't searched yet)

Greetings,

Daniel





--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
lingtools@sina.com
Guest
Posts: n/a
 
      03-01-2007

What about this ?
-------------------------
class B
{
protected:
B(const B&);
B& operator=(const B&);
public:
B(int);
};
class C : public B
{
public:
C(int);
};
int main()
{
C c = C(42); //
B b = B(42); //
}
------------------------
copy ctor of base class is set to protected.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inaccessible base despite public inheritance Stefan Weber C++ 9 05-29-2007 07:58 AM
virtual base class and construction/destruction order BeautifulMind C++ 7 02-08-2007 12:35 PM
'B' is an inaccessible base of 'D' yuvalif@gmail.com C++ 2 12-26-2005 07:55 AM
Default construction versus construction with initial values Ook C++ 10 10-08-2005 09:00 PM
Base class inaccessible due to ambiguity daniel.w.gelder@gmail.com C++ 4 05-04-2005 04:54 PM



Advertisments