473,569 Members | 2,880 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_EVALUATI ON_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest .c", line 35: error: "MyClass::MyCla ss(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 2401
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**********@g mail.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_EVALUATI ON_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest .c", line 35: error: "MyClass::MyCla ss(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_EVALUATI ON_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest .c", line 35: error: "MyClass::MyCla ss(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
1828
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 would be something like: sub func {
22
2834
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 function func(const String &param). When an actual String is passed as the param this is nice and efficient. I have a constructor which takes a...
42
5728
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...
3
2418
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
1765
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 possible damage to the object passed. If that is the case, why isnt the following program producing the unexpected o/p. I expect the program to print ...
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 copies of classes with members with static or dynamic memory allocation. However to do this it requires the programmer to override the default copy...
5
1686
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. My observation: 1. Compiler does not complain.
22
3598
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 v);
2
2434
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. ---------------------------------------------- #include <iostream> using namespace std;
0
7703
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...
0
7619
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...
0
7930
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. ...
0
7983
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...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
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...
0
3662
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...
1
2118
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
1
1229
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.