473,770 Members | 4,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++.m oderated. First time posters: Do this! ]

Feb 27 '07 #1
15 2408
Hello,

On Feb 26, 9:35 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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++.m oderated. First time posters: Do this! ]

Feb 27 '07 #2
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++.m oderated. First time posters: Do this! ]

Feb 27 '07 #3
On Feb 27, 3:35 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
[...]
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++.m oderated. First time posters: Do this! ]

Feb 27 '07 #4
On Feb 27, 1:35 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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++.m oderated. First time posters: Do this! ]

Feb 27 '07 #5
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++.m oderated. First time posters: Do this! ]

Feb 28 '07 #6
On Feb 27, 3:35 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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++.m oderated. First time posters: Do this! ]

Mar 1 '07 #7
On Mar 1, 8:55 am, "Old Wolf" <oldw...@inspir e.net.nzwrote:
On Feb 27, 3:35 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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++.m oderated. First time posters: Do this! ]

Mar 1 '07 #8
On 1 Mrz., 13:59, "James Kanze" <james.ka...@gm ail.comwrote:
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 Function2bool operator==(cons t function<Functi on2>&);
template<class Function2bool operator!=(cons t function<Functi on2>&);

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++.m oderated. First time posters: Do this! ]

Mar 1 '07 #9

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++.m oderated. First time posters: Do this! ]

Mar 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
5808
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
7
2568
by: madhukar_bm | last post by:
We know that copy constructor is used if a class object is passed by value to a function, is there any logic behind the use of copy constructor as assignment operator could also have been used instead of copy constructor Regards Madhukar
11
3437
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
10
4028
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
3
3912
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base* foo; which a complicated code allocates as one of derived's and sets up.
13
3971
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help is much appreciated. JD
6
1813
by: suresh | last post by:
Hi Could you please tell why copy constructor is not called in the first line in main(), as mentioned in the text books. I used g++ version 4.1.2 on debian etch thanks suresh # include <iostream>
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.