C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

returning object with private copy c-tor & private assignmen
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Dmitry
Guest





PostPosted: Thu Oct 28, 2004 1:43 pm    Post subject: returning object with private copy c-tor & private assignmen Reply with quote



Hi

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Basicly, since the Return Ralue Optimization(RVO) is always enabled
in modern compilers, there should be no need in copy c-tor when returning
unnamed object but the compiler(VC 7.1) prints the error even if copy c-tor
is not really used at all...

Thanks, Dmitry



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Fri Oct 29, 2004 2:11 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote



Dmitry wrote:
Quote:
Hi

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Basicly, since the Return Ralue Optimization(RVO) is always enabled
in modern compilers, there should be no need in copy c-tor when returning
unnamed object but the compiler(VC 7.1) prints the error even if copy c-tor
is not really used at all...

The compiler is obligated to check the access on the copy constructor
even if no copy is made. Your object forbids copies, you can't return
by value. You'll have to return a pointer or reference to the original
object.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
jnorberg@gmail.com
Guest





PostPosted: Fri Oct 29, 2004 2:12 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote



Hi,

I suggest that you return a pointer to the object (or rather a
boost::shared_ptr) if possible.
__
Jonas Norberg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Barbati
Guest





PostPosted: Fri Oct 29, 2004 2:14 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Dmitry wrote:
Quote:
Hi

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

If you mean return *by-value* the answer is no. What you can do is to
allocate the object on the heap and return an std::auto_ptr (or another
smart pointer type of your choice) to it.

Quote:
Basicly, since the Return Ralue Optimization(RVO) is always enabled
in modern compilers, there should be no need in copy c-tor when returning
unnamed object but the compiler(VC 7.1) prints the error even if copy c-tor
is not really used at all...

RVO is, as the name says, an *optimization*. Therefore, the fact that
it's used shall have no impact on the semantic of the program. As the
copy happens "conceptually" the copy ctor must be accessible, whether
the compiler decides to omit the call or not. In standardese (12.2/1):

"[...] Even when the creation of the temporary object is avoided (12.Cool,
all the semantic restrictions must be respected as if the temporary
object was created. [Example: even if the copy constructor is not
called, all the semantic restrictions, such as accessibility (clause
11), shall be satisfied. ]"

BTW, who said that RVO is *always* enabled in modern compilers? How can
you be so sure? ;-)

Regards,

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
jakacki
Guest





PostPosted: Fri Oct 29, 2004 2:15 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Quote:
I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.

If your function does not create the object, you can pass
it in and return by reference (like in operator<<).

Quote:
Basicly, since the Return Ralue Optimization(RVO) is
always enabled in modern compilers, there should be no
need in copy c-tor when returning unnamed object but the
compiler(VC 7.1) prints the error even if copy c-tor is
not really used at all...

Return Value Optimization is, well, optimization. Compiler
can employ as it pleases. That would be disastrous if
correctness of the code would rely on what a particular
compiler pleases.

BR
Grzegorz

--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Fri Oct 29, 2004 2:22 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Dmitry wrote:
Quote:
Hi

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Basicly, since the Return Ralue Optimization(RVO) is always enabled
in modern compilers, there should be no need in copy c-tor when returning
unnamed object but the compiler(VC 7.1) prints the error even if copy c-tor
is not really used at all...

private copy-ctor and op= are usually a clear indication that someone
has gone to some effort to make sure that you cannot do that.

Usually, it's because copying or assigning such object will break
something.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dmitry
Guest





PostPosted: Fri Oct 29, 2004 2:43 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote


I need to return by value for the object to be in one state only.
And returning pointer/reference leads to other complex issues..

Actually, I've found the workaround:
I'am declaring the public copy c-tor without defining it and
since it is not really needed the code compiles fine.

As for RVO - there is absolutely no reason, why the compiler decides
not to use it - it is even enabled when optimization is "off" in VC 7.1
I hope new versions of gcc has the same thing.

Also, I consider RVO and even NRVO to be not an optimization
but as all new compilers required feature. ;-)

P.S. Thanks for all replies!

Dmitry Sychov






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dave Harris
Guest





PostPosted: Sun Oct 31, 2004 11:22 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

[email]nospam (AT) nospam (DOT) com[/email] (Dmitry) wrote (abridged):
Quote:
I need to return the object which implements a private
copy constructior and private assignment operator.

You could try using placement-new, but it may be tricky to get correct and
safe. So this:

void test() {
Object obj = proc();
}

Object proc() {
return Object();
}

would become something like:

void test() {
char buf[ sizeof( Object ) ];
Object &obj = proc( buf );
obj.~Object();
}

Object &proc( void *p ) {
return *new(p) Object();
}

which is roughly what the compiler does when it wants to apply the Return
Value Optimisation. The tricky bits are getting the alignment of buf
correct, and ensuring that obj gets destroyed exactly when it needs to be,
even in the face of exceptions etc. And I may have the syntax wrong; I
don't often write stuff like that for real.

However, it seems to me that you are trying to do something which the
object was specifically intended to prevent. You ought to review the
design. If your code has a legitimate need, then the object class ought
either to make the copy constructor public, or else make your routine a
friend or member. Or maybe research one of the "move" or "pilfer" idioms
that get discussed in this forum from time to time.


Quote:
Basicly, since the Return Ralue Optimization(RVO) is always enabled
in modern compilers, there should be no need in copy c-tor when
returning unnamed object but the compiler(VC 7.1) prints the error
even if copy c-tor is not really used at all...

The compiler is correct.

-- Dave Harris, Nottingham, UK

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
steven11
Guest





PostPosted: Sun Oct 31, 2004 12:05 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Am 29 Oct 2004 10:15:17 -0400 schrieb jakacki <jakacki_hates_spam (AT) acm (DOT) org>:

Quote:
I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.


he isn't able to do that because the ctor is private...i suggest he isn't
the author of the class
and so he can't modify the access of the ctor to public


--
mfg Steven

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
jakacki
Guest





PostPosted: Mon Nov 01, 2004 11:48 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Quote:
I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.


he isn't able to do that because the ctor is private...

To do what exactly? Create on heap or return by
std::auto_ptr ? I do not see anything that would prevent
him from doing either.

BR
Grzegorz

--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jakacki
Guest





PostPosted: Mon Nov 01, 2004 11:49 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Quote:
I need to return by value for the object to be in one
state only.

How can object be in two states?

Quote:
And returning pointer/reference leads to other complex
issues..

Actually, I've found the workaround: I'am declaring the
public copy c-tor without defining it and since it is not
really needed the code compiles fine.

As for RVO - there is absolutely no reason, why the
compiler decides not to use it - it is even enabled when
optimization is "off" in VC 7.1 I hope new versions of
gcc has the same thing.

If you are writting code that is to be used by you only and
with one compiler only, that's perfectly OK.

If your code is to be maintained or extended by others and
possibly compiled with other compilers, then what you are
doing is very fragile. The first thing the maintainer will
do once linker reports missing symbol will be to write the
copy ctor body ("he is returning by value, so clearly he must have missed
the copy ctor body..."). This,
however, will break assumptions that you now make when
writting the code.

In a nutshell: such code non-portable and easy to break.

BR
Grzegorz

--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Mon Nov 01, 2004 11:51 am    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

steven11 wrote:
Quote:
Am 29 Oct 2004 10:15:17 -0400 schrieb jakacki <jakacki_hates_spam (AT) acm (DOT) org>:

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.


he isn't able to do that because the ctor is private...i suggest he isn't
the author of the class
and so he can't modify the access of the ctor to public


Earlier in this thread, he solved the problem by declaring the copy
ctor public, and leaving it unimplemented.

This allowed the compilation to proceed. The compiled code elides
the copy construction anyway because of RVO, so the there were
no unresolved references to the unimplemented copy ctor.

So, I guess the fellow owns the code, or can modify it.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Mon Nov 01, 2004 10:19 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

steven11 <steven11 (AT) gmx (DOT) de> wrote

Quote:
Am 29 Oct 2004 10:15:17 -0400 schrieb jakacki <jakacki_hates_spam (AT) acm (DOT) org>:

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.


he isn't able to do that because the ctor is private...i suggest he isn't
the author of the class
and so he can't modify the access of the ctor to public

The copy ctor and the assignment operator are private, but there may
be more - or there is a factory returning heap-allocated objects.
Roughly the only pattern in which you can't create an object is the
singleton pattern - but in that case, you don't have to return it.

HTH,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
steven11
Guest





PostPosted: Tue Nov 02, 2004 1:35 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Am 1 Nov 2004 06:51:44 -0500 schrieb Antoun Kanawati
<NO.antounk.SPAM (AT) comcast (DOT) net>:

Quote:
steven11 wrote:
Am 29 Oct 2004 10:15:17 -0400 schrieb jakacki
[email]jakacki_hates_spam (AT) acm (DOT) org[/email]>:

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.


he isn't able to do that because the ctor is private...i suggest he
isn't
the author of the class
and so he can't modify the access of the ctor to public


Earlier in this thread, he solved the problem by declaring the copy
ctor public, and leaving it unimplemented.

This allowed the compilation to proceed. The compiled code elides
the copy construction anyway because of RVO, so the there were
no unresolved references to the unimplemented copy ctor.

So, I guess the fellow owns the code, or can modify it.


sorry, it was my mistake Wink. i should have read the whole answers...

--
mfg Steven

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Tue Nov 02, 2004 1:36 pm    Post subject: Re: returning object with private copy c-tor & private assig Reply with quote

Antoun Kanawati <NO.antounk.SPAM (AT) comcast (DOT) net> writes:

Quote:
steven11 wrote:
Am 29 Oct 2004 10:15:17 -0400 schrieb jakacki <jakacki_hates_spam (AT) acm (DOT) org>:

I need to return the object which implements a private
copy constructior and private assignment operator.

Is is possible? Some tricks maybe?

Create on heap and return by std::auto_ptr.


he isn't able to do that because the ctor is private...i suggest he isn't
the author of the class
and so he can't modify the access of the ctor to public


Earlier in this thread, he solved the problem by declaring the copy
ctor public, and leaving it unimplemented.

This allowed the compilation to proceed. The compiled code elides
the copy construction anyway because of RVO, so the there were
no unresolved references to the unimplemented copy ctor.

Of course you realize that's totally non-portable?

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.