473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with a compile problem

I have some production code that doesn't compile with GCC 3.4 but
works with GCC 3.3, VC8, and Comeau, so I'm wondering if this is a
compiler issue or if I'm doing something wrong. Stripped down, this is
the code in question:

class Foo {
public:
Foo() {}
private:
Foo(const Foo&); // Line 5, noncopyable by design
};

const Foo& operator<<(const Foo& foo, int) {
return foo;
}

int main() {
Foo() << 42; // Line 13
}

The error is this:

test.cpp:5: error: `Foo::Foo(const Foo&)' is private
test.cpp:13: error: within this context

What I don't understand is why the compiler is trying to invoke the
copy ctor in the first place. It seems like there shouldn't be any
copying going on, so GCC shouldn't care that the copy ctor is private.

Thanks.

Derek

May 30 '07 #1
8 1232
de**********@grog.net wrote:
I have some production code that doesn't compile with GCC 3.4 but
works with GCC 3.3, VC8, and Comeau, so I'm wondering if this is a
compiler issue or if I'm doing something wrong. Stripped down, this is
the code in question:
Compiler issue - try using the latest gcc (4.1.2 or 4.2.0).
class Foo {
public:
Foo() {}
private:
Foo(const Foo&); // Line 5, noncopyable by design
};

const Foo& operator<<(const Foo& foo, int) {
return foo;
}

int main() {
Foo() << 42; // Line 13
}
Change your main to:

int main() {
Foo a;
a << 42; // Line 13
}

or change your class to this:

class Foo {
public:
Foo() {}
const Foo& operator<<(int) {
return *this;
}
private:
Foo(const Foo&); // Line 5, noncopyable by design
};

int main() {
Foo() << 42; // Line 13
}

What I don't understand is why the compiler is trying to invoke the
copy ctor in the first place. It seems like there shouldn't be any
copying going on, so GCC shouldn't care that the copy ctor is private.
You are creating temporary, but I am not sure what exactly is going on
May 30 '07 #2
anon wrote:
de**********@grog.net wrote:
>I have some production code that doesn't compile with GCC 3.4 but
works with GCC 3.3, VC8, and Comeau, so I'm wondering if this is a
compiler issue or if I'm doing something wrong. Stripped down, this is
the code in question:

Compiler issue - try using the latest gcc (4.1.2 or 4.2.0).
Sorry. To be more precise: you are doing something wrong, but it is a
compilers issue with gcc 3.3, VC8 and Comeau that they allow that thing
to be compiled.
May 30 '07 #3
de**********@grog.net wrote:
I have some production code that doesn't compile with GCC 3.4 but
works with GCC 3.3, VC8, and Comeau, so I'm wondering if this is a
compiler issue or if I'm doing something wrong. Stripped down, this is
the code in question:

class Foo {
public:
Foo() {}
private:
Foo(const Foo&); // Line 5, noncopyable by design
};

const Foo& operator<<(const Foo& foo, int) {
return foo;
}

int main() {
Foo() << 42; // Line 13
}

The error is this:

test.cpp:5: error: `Foo::Foo(const Foo&)' is private
test.cpp:13: error: within this context

What I don't understand is why the compiler is trying to invoke the
copy ctor in the first place. It seems like there shouldn't be any
copying going on, so GCC shouldn't care that the copy ctor is private.

Thanks.

Derek
There shouldn't be any copying involved, definitely a compiler issue in
my book.

F
May 30 '07 #4
On May 30, 12:07 pm, Fei Liu <fei...@aepnetworks.comwrote:
derek.goo...@grog.net wrote:
I have some production code that doesn't compile with GCC 3.4 but
works with GCC 3.3, VC8, and Comeau, so I'm wondering if this is a
compiler issue or if I'm doing something wrong. Stripped down, this is
the code in question:
class Foo {
public:
Foo() {}
private:
Foo(const Foo&); // Line 5, noncopyable by design
};
const Foo& operator<<(const Foo& foo, int) {
return foo;
}
int main() {
Foo() << 42; // Line 13
}
The error is this:
test.cpp:5: error: `Foo::Foo(const Foo&)' is private
test.cpp:13: error: within this context
What I don't understand is why the compiler is trying to invoke the
copy ctor in the first place. It seems like there shouldn't be any
copying going on, so GCC shouldn't care that the copy ctor is private.
Thanks.
Derek

There shouldn't be any copying involved, definitely a compiler issue in
my book.

F
That was my impression also. Naturally it wasn't hard to change the
code to work by using a named variable instead of a temporary, but it
didn't seem like the copy ctor should be in play. Both GCC 3.4 and
4.2 seem to want the copy ctor to be available, though GCC 3.3,
Comeau, and VC8/SP1 do not.

May 30 '07 #5
On May 30, 11:44 am, anon <a...@no.nowrote:
anon wrote:
derek.goo...@grog.net wrote:
I have some production code that doesn't compile with GCC 3.4 but
works with GCC 3.3, VC8, and Comeau, so I'm wondering if this is a
compiler issue or if I'm doing something wrong. Stripped down, this is
the code in question:
Compiler issue - try using the latest gcc (4.1.2 or 4.2.0).

Sorry. To be more precise: you are doing something wrong, but it is a
compilers issue with gcc 3.3, VC8 and Comeau that they allow that thing
to be compiled.
Not sure I understand. I'm not copying anything, so what exactly is
it that's wrong? That the class Foo is actually non-copyable by
design, and since there is no copy that I can see, the compiler should
not require a public copy ctor just because I'm using a temporary
object.

May 30 '07 #6
On May 30, 10:30 pm, derek.goo...@grog.net wrote:
Not sure I understand. I'm not copying anything, so what exactly is
it that's wrong? That the class Foo is actually non-copyable by
design, and since there is no copy that I can see, the compiler should
not require a public copy ctor just because I'm using a temporary
object.
Yes it should - in fact, it *has* to require an accessible copy ctor.

Your expression "Foo()" is an rvalue (section 3.10.6 of the standard).
When you pass it to your operator<<, you are binding an rvalue to a
const reference (covered by section 8.5.3 of the standard). In this
case the compiler is given 2 options by the standard:

1) bind the reference directly to the rvalue, or
2) construct a new temporary object from the the rvalue object, and
bind the reference to the temporary object.

As it happens, g++ chooses option 1. But the standard says "The
constructor that would be used to make the copy shall be callable
whether or not the copy is actually done." And you have made yours
private.

So g++ is correct to complain, and the other compilers are wrong to
accept your code. FWIW I don't understand the rationale for option (2)
above, but there you have it.

May 31 '07 #7

On 5/30/07 5:08 PM, in article
11**********************@w5g2000hsg.googlegroups.c om, "Pete C"
<5g*******@sneakemail.comwrote:
On May 30, 10:30 pm, derek.goo...@grog.net wrote:
>Not sure I understand. I'm not copying anything, so what exactly is
it that's wrong? That the class Foo is actually non-copyable by
design, and since there is no copy that I can see, the compiler should
not require a public copy ctor just because I'm using a temporary
object.

Yes it should - in fact, it *has* to require an accessible copy ctor.

Your expression "Foo()" is an rvalue (section 3.10.6 of the standard).
When you pass it to your operator<<, you are binding an rvalue to a
const reference (covered by section 8.5.3 of the standard). In this
case the compiler is given 2 options by the standard:

1) bind the reference directly to the rvalue, or
2) construct a new temporary object from the the rvalue object, and
bind the reference to the temporary object.
This requirement never made much sense - and it has in fact been eliminated
in the current C++ draft Standard. So I would recommend compiling with a C++
compiler that has been updated to the more sensible behavior.
As it happens, g++ chooses option 1. But the standard says "The
constructor that would be used to make the copy shall be callable
whether or not the copy is actually done." And you have made yours
private.
But is a public copy constructor that is never defined still "callable"? How
would the compiler know whether or not the constructor is callable without
actually calling it?
So g++ is correct to complain, and the other compilers are wrong to
accept your code. FWIW I don't understand the rationale for option (2)
above, but there you have it.
I doubt anyone else much understand this bizarre requirement either - which
is probably why it has been dropped.

Greg

May 31 '07 #8
On May 31, 2:08 am, Pete C <5gv7rq...@sneakemail.comwrote:
On May 30, 10:30 pm, derek.goo...@grog.net wrote:
Not sure I understand. I'm not copying anything, so what exactly is
it that's wrong? That the class Foo is actually non-copyable by
design, and since there is no copy that I can see, the compiler should
not require a public copy ctor just because I'm using a temporary
object.
Yes it should - in fact, it *has* to require an accessible copy ctor.
Your expression "Foo()" is an rvalue (section 3.10.6 of the standard).
When you pass it to your operator<<, you are binding an rvalue to a
const reference (covered by section 8.5.3 of the standard). In this
case the compiler is given 2 options by the standard:
1) bind the reference directly to the rvalue, or
2) construct a new temporary object from the the rvalue object, and
bind the reference to the temporary object.
As it happens, g++ chooses option 1. But the standard says "The
constructor that would be used to make the copy shall be callable
whether or not the copy is actually done." And you have made yours
private.
So g++ is correct to complain, and the other compilers are wrong to
accept your code. FWIW I don't understand the rationale for option (2)
above, but there you have it.
It's related to the lifetime of temporaries. Consider something
like the following:

struct B {} ;
struct A { B b ; } ;

A f() ;

void
g()
{
B const& b = f().b ;
// uses b...
}

According to the standard, the lifetime of the object bound to
the reference in g is until the end of the function. But the
lifetime of the A object returned by f() must end at the end of
teh full expression. And the only way to destruct the A object
without destructing the B object bound to b is by making a copy.
(Both G++ and VC++ do copy in the above case, as required by the
standard.)

Presumably, the authors of the standard didn't want to make
special cases, and describe in full detail when and when not the
copy would occur, so the simply made it up to the implementation
in all cases. And required the copy constructor to be
accessible just in case.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will Stuyvesant | last post by:
There seems to be no XML parser that can do validation in the Python Standard Libraries. And I am stuck with Python 2.1.1. until my web master upgrades (I use Python for CGI). I know pyXML has...
3
by: Troy | last post by:
I have a class library (DLL) that I created and have been using for sometime in my various .NET applications. Recently, I have been having problems with what I believe is corruption (or a bug) of the...
2
by: Song | last post by:
Hi, gurus, I am new with visual studio... I am trying to build a standalone C++ project by MS visual C++, the problem that I have is: if compile it as "Release" version, it crashes by a...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
13
by: Brett Baisley | last post by:
At school, we do all of our coding in emacs, but I am trying to get the example apps working at home using Visual C++.net. In the example, there are 4 .cpp files (canvas.cpp, main.cpp,...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
3
by: Tyler | last post by:
Can someone help by explaining why the following class will not compile (VS2005), and what can be done to pass the function pointer to the "C" method? When I compile the following program, I get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.