Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

g++ behavior while noncopyable class

5 views
Skip to first unread message

Alex Vinokur

unread,
Mar 28, 2012, 4:24:14 AM3/28/12
to avin...@amdocs.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

Alex Vinokur

unread,
Mar 28, 2012, 5:05:07 AM3/28/12
to
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;
}
// =============================


> 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

SG

unread,
Mar 28, 2012, 1:21:40 PM3/28/12
to
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

Alex Vinokur

unread,
Mar 28, 2012, 2:13:57 PM3/28/12
to
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

Miles Bader

unread,
Apr 2, 2012, 9:18:28 PM4/2/12
to
Alex Vinokur <alex.v...@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

Jorgen Grahn

unread,
Apr 3, 2012, 6:36:27 AM4/3/12
to
["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 .
0 new messages