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 

Pass-by-value variations

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
glenlow@pixelglow.com
Guest





PostPosted: Sat Mar 19, 2005 1:00 am    Post subject: Pass-by-value variations Reply with quote



Hi all,

Are the following three functions equivalent, in terms of observable
behavior re: the standard, and in terms of typical compiler
optimization?

// #1
template <typename T> void f (T x)
{
// change x
}

// #2
template <typename T> void f (const T& x)
{
T x2 = x;
// change x2
}

// #3
template <typename T> void f (T x)
{
T& x2 = x;
// change x2
}

#1 is classic "pass-by-value" and of course preferred if T is small,
inbuilt or MOJO/ZUTO-enhanced (?). However my immediate problem is that
in VC++ 7.1 some types cannot be passed by value -- specifically
aligned types. Rather than pepper my otherwise neat code with #ifdefs,
I thought I could work some template typedef magic to make it all work
out -- for aligned types in VC++ 7.1, it would come out as #2, but on
all other types or compilers it would come out as #3.

Now the efficiency of this on other compilers really hinges on whether
#3 is equivalent to #1. Or is #2 equivalent to #1 as well, so I should
just use #2 for all cases?

Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com


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

Back to top
Mike Jolley
Guest





PostPosted: Sun Mar 20, 2005 11:18 pm    Post subject: Re: Pass-by-value variations Reply with quote




<glenlow (AT) pixelglow (DOT) com> wrote

Quote:
Hi all,

Are the following three functions equivalent, in terms of observable
behavior re: the standard, and in terms of typical compiler
optimization?

// #1
template <typename T> void f (T x)
{
// change x
}

// #2
template <typename T> void f (const T& x)
{
T x2 = x;
// change x2
}

// #3
template <typename T> void f (T x)
{
T& x2 = x;
// change x2
}

#1 is classic "pass-by-value" and of course preferred if T is small,
inbuilt or MOJO/ZUTO-enhanced (?). However my immediate problem is that
in VC++ 7.1 some types cannot be passed by value -- specifically
aligned types. Rather than pepper my otherwise neat code with #ifdefs,
I thought I could work some template typedef magic to make it all work
out -- for aligned types in VC++ 7.1, it would come out as #2, but on
all other types or compilers it would come out as #3.

Now the efficiency of this on other compilers really hinges on whether
#3 is equivalent to #1. Or is #2 equivalent to #1 as well, so I should
just use #2 for all cases?

I'd go with #2 in all cases, heh. #3 is weird because you're declaring a
local
reference which nobody ever does. #1 and #3 you're passing an object by
copy that you might as well pass by reference. Copy constructors often do
interesting things that you don't want to happen when you just want to pass
a thing to a function. For example, a std::string will typically invoke
copy-
on-write behavior when it's copied like this. You'd probably prefer that
the thing just gets into the function in the most direct way possible, i.e.
by
reference.


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

Back to top
Ben Hutchings
Guest





PostPosted: Tue Mar 22, 2005 1:59 am    Post subject: Re: Pass-by-value variations Reply with quote



[email]glenlow (AT) pixelglow (DOT) com[/email] wrote:
Quote:
Hi all,

Are the following three functions equivalent, in terms of observable
behavior re: the standard, and in terms of typical compiler
optimization?

// #1
template <typename T> void f (T x)
{
// change x
}

This might not make a copy of the argument if it's a temporary.

Quote:
// #2
template <typename T> void f (const T& x)
{
T x2 = x;
// change x2
}

This always makes a copy of the argument.

Quote:
// #3
template <typename T> void f (T x)
{
T& x2 = x;
// change x2
}

I think this is equivalent to #1.

Quote:
#1 is classic "pass-by-value" and of course preferred if T is small,
inbuilt or MOJO/ZUTO-enhanced (?).
snip


Given the need to change the argument, I would say it is generally
preferable.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

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


Back to top
johnchx2@yahoo.com
Guest





PostPosted: Wed Mar 23, 2005 10:15 am    Post subject: Re: Pass-by-value variations Reply with quote

[email]glenlow (AT) pixelglow (DOT) com[/email] wrote:

Quote:
Now the efficiency of this on other compilers really hinges
on whether #3 is equivalent to #1. Or is #2 equivalent to #1
as well, so I should just use #2 for all cases?

I think that #2 could prevent some optimizations in some situations,
because the compiler can't guarantee that f() doesn't modify its
operand (via a const_cast). (Well, it can if the body of f() is
visible and if it bothers to check, but it certainly has to work
harder.) So it may not be a good idea to use #2 in all cases.

I also believe that #3 should almost certainly reduce to #1. x2 is
just another name for the same slot in the stack frame as x, so they're
indistinguishable once the compiler gets around to emitting assembly
instructions.


[ 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
Page 1 of 1

 
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.