Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #15150 > unrolled thread
| Started by | Alex Vinokur <alex.vinokur@gmail.com> |
|---|---|
| First post | 2012-03-28 01:24 -0700 |
| Last post | 2012-04-03 10:36 +0000 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.c++
g++ behavior while noncopyable class Alex Vinokur <alex.vinokur@gmail.com> - 2012-03-28 01:24 -0700
Re: g++ behavior while noncopyable class Alex Vinokur <alex.vinokur@gmail.com> - 2012-03-28 02:05 -0700
Re: g++ behavior while noncopyable class Alex Vinokur <alex.vinokur@gmail.com> - 2012-03-28 11:13 -0700
Re: g++ behavior while noncopyable class Miles Bader <miles@gnu.org> - 2012-04-03 10:18 +0900
Re: g++ behavior while noncopyable class SG <s.gesemann@gmail.com> - 2012-03-28 10:21 -0700
Re: g++ behavior while noncopyable class Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-03 10:36 +0000
| From | Alex Vinokur <alex.vinokur@gmail.com> |
|---|---|
| Date | 2012-03-28 01:24 -0700 |
| Subject | g++ behavior while noncopyable class |
| Message-ID | <7d36f4e9-f87e-47c5-be20-3e80e1063323@v2g2000vbx.googlegroups.com> |
Hi,
While compiling program below, behavior of g++ differs from Intel and
aCC HP compilers.
g++ detects error.
Any suggestions?
Thanks
// =====================================
// File test1.cpp
// --------------
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private:
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};
// --------------
class Foo : public noncopyable
{
public:
Foo (const Foo& i_foo, int i_int)
{
}
static Foo func1(const Foo& i_foo, int i_int)
{
return Foo(i_foo, i_int);
}
};
// --------------
int main()
{
return 0;
}
// =====================================
-------------------------
Compilers:
Intel compiler:
> icpc -V
Intel(R) C++ Intel(R) 64 Compiler XE for applications running on
Intel(R) 64, Version 12.0.4.191 Build 20110427
Copyright (C) 1985-2011 Intel Corporation. All rights reserved.
aCC HP compiler:
> aCC -V
aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]
GNU g++ compiler:
> g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --
disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-
gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)
-------------------------
Compilation.
> icpc test1.cpp
// No errors
> aCC test1.cpp
// No errors
> g++ test1.cpp
test1.cpp: In copy constructor Foo::Foo(const Foo&):
test1.cpp:9: error: noncopyable::noncopyable(const noncopyable&) is
private
test1.cpp:15: error: within this context
test1.cpp: In static member function static Foo Foo::func1(const Foo&,
int):
test1.cpp:23: note: synthesized method Foo::Foo(const Foo&) first
required here
[toc] | [next] | [standalone]
| From | Alex Vinokur <alex.vinokur@gmail.com> |
|---|---|
| Date | 2012-03-28 02:05 -0700 |
| Message-ID | <8c8a6fd5-ba3e-41db-aaa9-6c3749b96a46@db5g2000vbb.googlegroups.com> |
| In reply to | #15150 |
On Mar 28, 10:24 am, Alex Vinokur <alex.vino...@gmail.com> wrote:
> Hi,
>
> While compiling program below, behavior of g++ differs from Intel and
> aCC HP compilers.
> g++ detects error.
>
> Any suggestions?
>
The same problem with simpler program.
// ============================
// File test2.cpp
// --------------
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private:
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};
// --------------
class Foo : private noncopyable
{
public:
Foo (){}
};
// -------------
static Foo func1()
{
return Foo();
}
// --------------
int main()
{
return 0;
}
// =============================
-------------------------
Compilers:
Intel compiler:
> icpc -V
Intel(R) C++ Intel(R) 64 Compiler XE for applications running on
Intel(R) 64, Version 12.0.4.191 Build 20110427
Copyright (C) 1985-2011 Intel Corporation. All rights reserved.
aCC HP compiler:
> aCC -V
aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]
GNU g++ compiler:
> g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --
disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-
gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)
-------------------------
Compilation.
> icpc test2.cpp
// No errors
> aCC test2.cpp
// No errors
> g++ test2.cpp
test2.cpp: In copy constructor Foo::Foo(const Foo&):
test2.cpp:11: error: noncopyable::noncopyable(const noncopyable&) is
private
test2.cpp:17: error: within this context
test2.cpp: In function Foo func1():
test2.cpp:25: note: synthesized method Foo::Foo(const Foo&) first
required here
[toc] | [prev] | [next] | [standalone]
| From | Alex Vinokur <alex.vinokur@gmail.com> |
|---|---|
| Date | 2012-03-28 11:13 -0700 |
| Message-ID | <0962750e-94d5-4357-9e95-3b4f5cc4d7f2@i18g2000vbx.googlegroups.com> |
| In reply to | #15151 |
Thank you.
It is not my code. It is code of very large project.
Actually I think we can't change 'return by value' to something else.
It seems we need to remove 'noncopyable' for g++.
What does standard of C++03 write of this?
Alex
On Mar 28, 7:21 pm, SG <s.gesem...@gmail.com> wrote:
> On 28 Mrz., 11:05, Alex Vinokur wrote:
>
> > On Mar 28, 10:24 am, Alex Vinokur wrote:
>
> > > While compiling program below, behavior of g++ differs from Intel and
> > > aCC HP compilers.
> > > g++ detects error.
>
> > > Any suggestions?
>
> Yes: Write legal C++ code.
>
> If you want a function to return an object of your class by value, you
> better make this class at least movable. The other compilers probably
> complain because they simply don't care and just apply copy elision.
> However, the standard requires the class to be at least movable (C+
> +2011) or copyable (C++2003) regardless of wether a compiler does RVO
> or not (RVO = return value optimization).
>
>
>
>
>
>
>
>
>
> > The same problem with simpler program.
>
> > class noncopyable
> > {
> > protected:
> > noncopyable() {}
> > ~noncopyable() {}
> > private:
> > noncopyable( const noncopyable& );
> > const noncopyable& operator=( const noncopyable& );
> > };
>
> > class Foo : private noncopyable
> > {
> > public:
> > Foo (){}
> > };
>
> > Foo func1()
> > {
> > return Foo();
> > }
>
> > [...]
>
> Cheers!
> SG
[toc] | [prev] | [next] | [standalone]
| From | Miles Bader <miles@gnu.org> |
|---|---|
| Date | 2012-04-03 10:18 +0900 |
| Message-ID | <87wr5x62aj.fsf@catnip.gol.com> |
| In reply to | #15155 |
Alex Vinokur <alex.vinokur@gmail.com> writes:
> It is not my code. It is code of very large project.
> Actually I think we can't change 'return by value' to something else.
> It seems we need to remove 'noncopyable' for g++.
>
> What does standard of C++03 write of this?
My memory is the same as SG: even if the copy is elided by
optimization, the class is still required to be "potentially copyable"
(in C++98/C++03).
I think many compilers were traditionally pretty lax, so source code
like this could slip through if the copy was not needed, but it was
always invalid.
Note that Clang (which, like recent versions of g++, does a pretty
good job of following the standard closely) gives the same error as
g++:
$ clang++ -o uc -O2 uc.cc
uc.cc:15:7: error: base class 'noncopyable' has private copy constructor
class Foo : private noncopyable
^
uc.cc:10:7: note: declared private here
noncopyable( const noncopyable& );
^
uc.cc:24:9: note: implicit default copy constructor for 'Foo' first
required here
return Foo();
^
1 error generated.
-miles
--
The secret to creativity is knowing how to hide your sources.
--Albert Einstein
[toc] | [prev] | [next] | [standalone]
| From | SG <s.gesemann@gmail.com> |
|---|---|
| Date | 2012-03-28 10:21 -0700 |
| Message-ID | <4698fe3a-db2f-45f2-b4df-68fbfd8c1d3a@er9g2000vbb.googlegroups.com> |
| In reply to | #15151 |
On 28 Mrz., 11:05, Alex Vinokur wrote:
> On Mar 28, 10:24 am, Alex Vinokur wrote:
>
> > While compiling program below, behavior of g++ differs from Intel and
> > aCC HP compilers.
> > g++ detects error.
>
> > Any suggestions?
Yes: Write legal C++ code.
If you want a function to return an object of your class by value, you
better make this class at least movable. The other compilers probably
complain because they simply don't care and just apply copy elision.
However, the standard requires the class to be at least movable (C+
+2011) or copyable (C++2003) regardless of wether a compiler does RVO
or not (RVO = return value optimization).
> The same problem with simpler program.
>
> class noncopyable
> {
> protected:
> noncopyable() {}
> ~noncopyable() {}
> private:
> noncopyable( const noncopyable& );
> const noncopyable& operator=( const noncopyable& );
> };
>
> class Foo : private noncopyable
> {
> public:
> Foo (){}
> };
>
> Foo func1()
> {
> return Foo();
> }
>
> [...]
Cheers!
SG
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2012-04-03 10:36 +0000 |
| Message-ID | <slrnjnlkla.1ls.grahn+nntp@frailea.sa.invalid> |
| In reply to | #15150 |
["Followup-To:" header set to comp.lang.c++.] On Wed, 2012-03-28, Alex Vinokur wrote: > Hi, > > While compiling program below, behavior of g++ differs from Intel and > aCC HP compilers. > g++ detects error. ... >> icpc test1.cpp > // No errors > >> aCC test1.cpp > // No errors > >> g++ test1.cpp At least with g++, you really *need* to provide flags to set the warning level and C++ standard level, if you're at all interested in getting warnings about broken code. A good starting point: g++ -Wall -Wextra -pedantic -Wold-style-cast -std=c++98 -O3 I expect the other two compilers to behave in a similar way. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web