Topic: noncopyable problems
Author: Sam <sakarab@yahoo.com>
Date: Sat, 26 May 2007 07:58:53 CST Raw View
Hello!
In my last project I have code like the following:
class A
{
private:
int mVal;
// non copyable
A( const A& src );
A& operator=( const A& src );
public:
A( int val ) : mVal(val) {} // empty
int GetValue() const { return mVal; }
};
class B
{
private:
int mVal;
public:
B( const A& a ) : mVal(a.GetValue()) {} // empty
};
void foo_foo( const A& a )
{
}
void foo( const A& a )
{
foo_foo( a );
}
int main()
{
// B b( A( 5 ) ); // error
// foo( A( 5 ) ); // error
A a( 5 ); // compiles
foo( a ); // compiles
B bb( a ); // compiles
return 0;
}
All of the compilers I tested, accepted the above code with no problem, except
gcc (the lines commented as "error"). Tested versions 3.4.x and 4.0.x.
Further investigating the problem I found these posts:
http://groups.google.com/group/comp.lang.c++/browse_frm/thread/a02c68db35a9f836/89c0badc3ee265e8?tvc=1&q=%22is+private%22+gcc&hl=en#89c0badc3ee265e8
http://groups.google.com/group/comp.std.c++/browse_frm/thread/357fa8c64351ee3d/332680eba3c0be52?tvc=1&hl=en#332680eba3c0be52
http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_frm/thread/7f1bb8902ae9d43/77b8cf941ea27dd1?tvc=1#77b8cf941ea27dd1
(short form of the above links)
http://tinyurl.com/2lqtyt
http://tinyurl.com/352xw2
http://tinyurl.com/2r7jmv
After reading them, my understanding is that a standard conforming compiler
*must* check for the existence of a copy constructor in the above case. Which
leads me to the conclusion that this makes the "noncopyable" thing useless.
1.Is my understanding right?
2.Is there another way to express the noncopyable "nature" of a class?
3.Is GCC wrong?
4.Is the standard "wrong"? :-)
Thanks in advance.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: howard.hinnant@gmail.com (Howard Hinnant)
Date: Sat, 26 May 2007 21:15:00 GMT Raw View
In article <f38qnk$4dl$1@mouse.otenet.gr>, Sam <sakarab@yahoo.com>
wrote:
> Hello!
>
> In my last project I have code like the following:
>
> class A
> {
> private:
> int mVal;
> // non copyable
> A( const A& src );
> A& operator=( const A& src );
> public:
> A( int val ) : mVal(val) {} // empty
> int GetValue() const { return mVal; }
> };
>
> class B
> {
> private:
> int mVal;
> public:
> B( const A& a ) : mVal(a.GetValue()) {} // empty
> };
>
> void foo_foo( const A& a )
> {
> }
>
> void foo( const A& a )
> {
> foo_foo( a );
> }
>
> int main()
> {
> // B b( A( 5 ) ); // error
> // foo( A( 5 ) ); // error
> A a( 5 ); // compiles
> foo( a ); // compiles
> B bb( a ); // compiles
> return 0;
> }
>
> All of the compilers I tested, accepted the above code with no problem,
> except
> gcc (the lines commented as "error"). Tested versions 3.4.x and 4.0.x.
>
> Further investigating the problem I found these posts:
>
> http://groups.google.com/group/comp.lang.c++/browse_frm/thread/a02c68db35a9f83
> 6/89c0badc3ee265e8?tvc=1&q=%22is+private%22+gcc&hl=en#89c0badc3ee265e8
> http://groups.google.com/group/comp.std.c++/browse_frm/thread/357fa8c64351ee3d
> /332680eba3c0be52?tvc=1&hl=en#332680eba3c0be52
> http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_frm/thread/7f1
> bb8902ae9d43/77b8cf941ea27dd1?tvc=1#77b8cf941ea27dd1
>
> (short form of the above links)
>
> http://tinyurl.com/2lqtyt
> http://tinyurl.com/352xw2
> http://tinyurl.com/2r7jmv
>
> After reading them, my understanding is that a standard conforming compiler
> *must* check for the existence of a copy constructor in the above case. Which
> leads me to the conclusion that this makes the "noncopyable" thing useless.
>
> 1.Is my understanding right?
Yes.
> 2.Is there another way to express the noncopyable "nature" of a class?
Not in C++03.
> 3.Is GCC wrong?
No.
> 4.Is the standard "wrong"? :-)
Yes (at least for some definition of "wrong"). And it is being changed
for C++0X. Here is an experimental gcc C++0X compiler which compiles
your example:
http://www.generic-programming.org/software/ConceptGCC/
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Mathias Gaunard <loufoque@gmail.com>
Date: Sat, 26 May 2007 16:16:46 CST Raw View
On May 26, 3:58 pm, Sam <saka...@yahoo.com> wrote:
> After reading them, my understanding is that a standard conforming compiler
> *must* check for the existence of a copy constructor in the above case. Which
> leads me to the conclusion that this makes the "noncopyable" thing useless.
>
> 1.Is my understanding right?
> 2.Is there another way to express the noncopyable "nature" of a class?
> 3.Is GCC wrong?
> 4.Is the standard "wrong"? :-)
Indeed, the standard requires that an object is copiable to make an
rvalue into a reference. That means GCC is right, and the other
compilers you tried are bogus.
Be aware, though, that there is no such requirement with rvalue
references.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: sakarab@yahoo.com (Sam)
Date: Sun, 27 May 2007 17:03:46 GMT Raw View
Howard Hinnant wrote:
> In article <f38qnk$4dl$1@mouse.otenet.gr>, Sam <sakarab@yahoo.com>
> wrote:
>
>> 4.Is the standard "wrong"? :-)
>
> Yes (at least for some definition of "wrong"). And it is being changed
> for C++0X. Here is an experimental gcc C++0X compiler which compiles
> your example:
>
> http://www.generic-programming.org/software/ConceptGCC/
This is a releaf. At least my code will compile "proprtly" some years
later. Do you have a link of the proposal/defect report/paper (I
don't know how to call it) for this change?
Thanks!
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Sun, 27 May 2007 13:29:51 CST Raw View
In article <f3c8t2$flh$1@mouse.otenet.gr>, sakarab@yahoo.com (Sam)
wrote:
> Howard Hinnant wrote:
> > In article <f38qnk$4dl$1@mouse.otenet.gr>, Sam <sakarab@yahoo.com>
> > wrote:
> >
> >> 4.Is the standard "wrong"? :-)
> >
> > Yes (at least for some definition of "wrong"). And it is being changed
> > for C++0X. Here is an experimental gcc C++0X compiler which compiles
> > your example:
> >
> > http://www.generic-programming.org/software/ConceptGCC/
>
> This is a releaf. At least my code will compile "proprtly" some years
> later. Do you have a link of the proposal/defect report/paper (I
> don't know how to call it) for this change?
I believe this is it:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#391
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]