Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > gcc needs copy consturctor for inplace constructed object passed to func as reference, others not

Reply
Thread Tools

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

 
 
nutty
Guest
Posts: n/a
 
      06-15-2006


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

 
Reply With Quote
 
 
 
 
Butterfly
Guest
Posts: n/a
 
      06-15-2006
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

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      06-15-2006
Butterfly <> 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)


 
Reply With Quote
 
Markus Schoder
Guest
Posts: n/a
 
      06-15-2006
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.

 
Reply With Quote
 
Markus Schoder
Guest
Posts: n/a
 
      06-15-2006
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.

 
Reply With Quote
 
nutty
Guest
Posts: n/a
 
      06-15-2006

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

 
Reply With Quote
 
Tom Widmer
Guest
Posts: n/a
 
      06-15-2006
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
 
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
Cloning classes, deep copy revisited (Was: Copy Consturctor in Perl) .. sln@netherlands.com Perl Misc 0 11-23-2008 08:33 PM
howto check is object a func, lambda-func or something else? dmitrey Python 3 04-29-2007 07:07 PM
Experimental only: Pointer copy consturctor does not work nagrik C++ 5 10-25-2006 10:36 AM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
What's the difference between built-in func getattr() and normal call of a func of a class Johnny Python 3 08-23-2005 08:28 AM



Advertisments