473,785 Members | 2,607 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
15 2409
On Mar 1, 4:03 pm, "Daniel Krügler" <daniel.krueg.. .@googlemail.co m>
wrote:
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>&);
I'm not sure whether I misunderstood James or you did. Just to be
sure, in my opinion the following invokes undefined behaviour:

class A {
public:
A();
};

class B : public A {
};

int main() {
B b;
}

A compiler might be able to generate a definition for B::B(), however
this would contain a call to A::A() which cannot be resolved by the
linker. This not very different from the following program:

void f();

int main() {
f();
}

The following, instead, is downright malformed:

class C {
private:
C();
};

class D : public C {
};

int main() {
D d;
}

The compiler is not allowed to generate a definition for D::D()
because C::C() is not accessible. In a way malformation kicks in
before undefined behaviour does.

Cheers,
Nicola Musatti

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

Mar 1 '07 #11
li*******@sina. com wrote:
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.
The construction of 'c' should compile, the construction of
'b' should not, IMO. 'C' has an implicit copy c-tor, which
calls the explicit copy c-tor for 'B', which it can do because
the protected access is granted to 'C'.

The construction of 'b' should NOT compile because it involves
making a copy of the temporary, and 'main' has no access to the
protected copy c-tor in 'B'.

I can see the correct behaviour in Comeau, but I have no way of
verifying this with GNU C++ at this time.

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! ]

Mar 1 '07 #12
On 1 Mrz., 18:56, "Nicola Musatti" <nicola.musa... @gmail.comwrote :
I'm not sure whether I misunderstood James or you did. Just to be
sure, in my opinion the following invokes undefined behaviour:
I'm quite sure that you didn't misunderstood James. Don't ask me why,
but I got totally lost in an arcane idea.
Fact is, as you correctly said, that an inaccessible and not defined
class member is (normally) harmless, because access rules apply before
the compiler has finished. What I indeed never consciously realised,
(but this was not what I meant in my previous posting) was that an
used but undefined entity (e.g. usual function or function member)
causes UB, most probably because I always relied on the linker to
recognize such situations. What I want to say is, that according to
the outcome of this discussion even

class T {
T(); // not defined;
public:
static void foo() {
T item;
}
};

int main() {
T::foo();
}

or even simpler

void foo(); // not defined

int main() {
foo();
}

causes UB. I assume that this surprising result
(at least for me) is related to the fact that the
C++ standard does not describe a linker model.
The existence of the linker and the occurence of
a linker error here and there seems so natural
for most of us that I never considered its cause
to be a programmers fault which actually belongs
to the UB family.

Greetings from Bremen,

Daniel Krügler

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

Mar 2 '07 #13
On Mar 1, 4:03 pm, "Daniel Krügler" <daniel.krueg.. .@googlemail.co m>
wrote:
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):
I'm afraid I wasn't very clear. Old Wolf said "The compiler
should generate an error because you haven't provided a body for
the constructor of C." That's really the only statement I'm
responding to; not providing bodies (or providing multiple
bodies) to a function that is actually used is a violation of
the one definition rule, and as such is undefined behavior.
(Not providing bodies to a function which is not actually used
is perfectly legal. Applying the standard's definition of
"used", i.e. virtual functions are always used, as is a user
declared operator delete, and maybe some other special cases.)

The original code requires a diagnostic (and will require one
even if bodies are provided for all of the user defined
functions) because the compiler is required to generated the
definition of the implicite copy constructor, and can't.

--
James Kanze (Gabi Software) email: ja*********@gma il.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 3 '07 #14
On Mar 2, 12:28 pm, "Daniel Krügler" <daniel.krueg.. .@googlemail.co m>
wrote:
On 1 Mrz., 18:56, "Nicola Musatti" <nicola.musa... @gmail.comwrote :
I'm not sure whether I misunderstood James or you did. Just to be
sure, in my opinion the following invokes undefined behaviour:
I'm quite sure that you didn't misunderstood James. Don't ask me why,
but I got totally lost in an arcane idea.
When I reread my statement, I got confused by it myself, so the
problem is on my side.
Fact is, as you correctly said, that an inaccessible and not defined
class member is (normally) harmless, because access rules apply before
the compiler has finished. What I indeed never consciously realised,
(but this was not what I meant in my previous posting) was that an
used but undefined entity (e.g. usual function or function member)
causes UB, most probably because I always relied on the linker to
recognize such situations. What I want to say is, that according to
the outcome of this discussion even
class T {
T(); // not defined;
public:
static void foo() {
T item;
}
};
int main() {
T::foo();

}
or even simpler
void foo(); // not defined
int main() {
foo();

}
causes UB.
That's correct. In practice, in this particular case, you will
certainly get an error from the linker (although I once used a
linker, many, many years ago, that would occasionally use the
address 0, rather than reporting an undefined external).
I assume that this surprising result
(at least for me) is related to the fact that the
C++ standard does not describe a linker model.
The existence of the linker and the occurence of
a linker error here and there seems so natural
for most of us that I never considered its cause
to be a programmers fault which actually belongs
to the UB family.
I think that the real reason is that this is part of a larger
rule, the one definition rule, and there are some violations of
this rule which are not easily detected. If, for example,
instead of providing no definition, you provide two, and you
link through libraries. It wouldn't be too difficult for the
standard to require a diagnostic in the case of a missing
definition; it may not describe the exact linker model, but it
does talk about resolving external symbols, and such, in the
phases of compilation. It's probably not worth addressing now,
however; hopefully, the next version of the standard will
contain some support for dynamic linking, and then, you do sort
of get undefined behavior.

--
James Kanze (Gabi Software) email: ja*********@gma il.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 3 '07 #15
In article <er**********@n ews.datemas.de> ,
Victor Bazarov <v.********@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
}

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?
Sorry for the delay in responding. I started mulling it and
got caught up in other things. I don't think you err:
The standard seems pretty clear that it needs to be accessible
in your context, even if it can be sidestepped regarding its "need".

Kanze seems to have the answer I would have provided (thanks James:)
It's a more deeper issue internally and as with all reports
it will be given consideration at some point.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

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

Mar 4 '07 #16

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
2569
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
3438
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
3972
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
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
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
10155
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
10095
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
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.