473,507 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcc needs copy consturctor for inplace constructed object passed to func as reference, others not



Hi all,

I have the following problem ( explanation below code ):

// main.cpp

class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private: // emphasize the following members are private
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};

class MyClass: noncopyable
{
public:
MyClass( ) { }
};

class IFormatter
{
public:
void func( const MyClass& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) ); // doesn't work
f.func( mc ); // works
return 0;
}

This code is accepted by comeau in strict mode and by all MSVC that I
had used 7.0 up to 8.0
..

gcc 4.0.2 complains that the copy constructor of MyClass is private.

Now I wannted to further simplify by removing the noncopyable base and
instead making the copy and assignment private in MyClass.
class MyClass
{
public:
MyClass( ) { }
private:
MyClass( MyClass const& );
MyClass const& operator=( MyClass const& );
};

class IFormatter
{
public:
void func( MyClass const& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) );
return 0;
}
surprisingly, now even comeau complains:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 35: error: "MyClass::MyClass(const MyClass &)",
required for
copy that was eliminated, is inaccessible
f.func( MyClass( ) );
^
Could it be possible that comeau is wrong for one or the other example?
Or did I change something significantly in my simplification?

Could someone try this with a gcc 4.1.x or 3.x.x ?

I appreciate your help and thank you in advance.

Ingo

Jun 15 '06 #1
6 2393
when you call f.func() like this

f.func( MyClass( ) );

compiler has to create tempolary object, that means copy contructor
will be called. You declare copy constructor as a private, so that's
why compiler complains you

Jun 15 '06 #2
Butterfly <Ma**********@gmail.com> wrote:
when you call f.func() like this

f.func( MyClass( ) );

compiler has to create tempolary object, that means copy contructor
will be called. You declare copy constructor as a private, so that's
why compiler complains you


I do not see why the copy ctor would have to be called. Afaik, the
temporary is created using the ctor taking no arguments, and a const
reference to this object is passed. No object copying involved.

regards
--
jb

(reply address in rot13, unscramble first)
Jun 15 '06 #3
nutty wrote:
Hi all,

I have the following problem ( explanation below code ):

// main.cpp

class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private: // emphasize the following members are private
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};

class MyClass: noncopyable
{
public:
MyClass( ) { }
};

class IFormatter
{
public:
void func( const MyClass& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) ); // doesn't work
f.func( mc ); // works
return 0;
}

This code is accepted by comeau in strict mode and by all MSVC that I
had used 7.0 up to 8.0
.

gcc 4.0.2 complains that the copy constructor of MyClass is private.

Now I wannted to further simplify by removing the noncopyable base and
instead making the copy and assignment private in MyClass.
class MyClass
{
public:
MyClass( ) { }
private:
MyClass( MyClass const& );
MyClass const& operator=( MyClass const& );
};

class IFormatter
{
public:
void func( MyClass const& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) );
return 0;
}
surprisingly, now even comeau complains:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 35: error: "MyClass::MyClass(const MyClass &)",
required for
copy that was eliminated, is inaccessible
f.func( MyClass( ) );
^
Could it be possible that comeau is wrong for one or the other example?
Or did I change something significantly in my simplification?
Binding an rvalue to a const reference invokes implementation-defined
behaviour. An implementation can bind directly to the object or copy
construct a temporary object. Comeau obviously does one or the other
depending on the context.

In other words for your code to be portable the copy constructor must
be accessible.
Could someone try this with a gcc 4.1.x or 3.x.x ?


gcc 4.1.1 complains as well in both cases.

Jun 15 '06 #4
Markus Schoder wrote:
nutty wrote:
Hi all,

I have the following problem ( explanation below code ):

// main.cpp

class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private: // emphasize the following members are private
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};

class MyClass: noncopyable
{
public:
MyClass( ) { }
};

class IFormatter
{
public:
void func( const MyClass& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) ); // doesn't work
f.func( mc ); // works
return 0;
}

This code is accepted by comeau in strict mode and by all MSVC that I
had used 7.0 up to 8.0
.

gcc 4.0.2 complains that the copy constructor of MyClass is private.

Now I wannted to further simplify by removing the noncopyable base and
instead making the copy and assignment private in MyClass.
class MyClass
{
public:
MyClass( ) { }
private:
MyClass( MyClass const& );
MyClass const& operator=( MyClass const& );
};

class IFormatter
{
public:
void func( MyClass const& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) );
return 0;
}
surprisingly, now even comeau complains:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 35: error: "MyClass::MyClass(const MyClass &)",
required for
copy that was eliminated, is inaccessible
f.func( MyClass( ) );
^
Could it be possible that comeau is wrong for one or the other example?
Or did I change something significantly in my simplification?


Binding an rvalue to a const reference invokes implementation-defined
behaviour. An implementation can bind directly to the object or copy
construct a temporary object. Comeau obviously does one or the other
depending on the context.

In other words for your code to be portable the copy constructor must
be accessible.


Actually from the wording in the standard it looks like the copy
constructor must be accessible in all cases even if the copy is not
done which would mean that Comeau gets this wrong.

Jun 15 '06 #5

Markus Schoder schrieb:


Actually from the wording in the standard it looks like the copy
constructor must be accessible in all cases even if the copy is not
done which would mean that Comeau gets this wrong.


Thank you,

your answers, both, finally helped.

So that means for me, either I need to avoid this syntax or I make the
copy constructor available, knowing that at least in release mode no
compiler would ever use it.

Ingo

Jun 15 '06 #6
Markus Schoder wrote:
Actually from the wording in the standard it looks like the copy
constructor must be accessible in all cases even if the copy is not
done which would mean that Comeau gets this wrong.


Indeed, Daveed Vandevoorde admitted the bug in this post:

http://groups.google.co.uk/group/com...b0ffb131e94bea

Tom
Jun 15 '06 #7

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

Similar topics

15
1816
by: Torsten Mohr | last post by:
Hi, i'd like to pass a reference or a pointer to an object to a function. The function should then change the object and the changes should be visible in the calling function. In perl this...
22
2822
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member...
42
5720
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...
3
2416
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
3
1759
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
8
449
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
5
1681
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. ...
22
3587
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
2425
by: sanjay | last post by:
Hi All, I have a doubt in understanding the output of the following program that i executed on my system. I was using DevC++ IDE which uses minGW based compiler. ...
0
7223
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,...
0
7377
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...
1
7034
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...
0
7488
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...
0
5623
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,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
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...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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...

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.