• Advertisement

Archived

This topic is now archived and is closed to further replies.

Conversion to reference

This topic is 5300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again, I am using references to specify function arguments quite a bit lately under the (possibly misguided) impression that it is a converient way to avoid copying of large objects. Anyway this little program:
#include <iostream>

using namespace std;

float Foo(float& arg) {
    return arg;
}

int main(void) {

    float a = 2.0;

    cout << Foo(a) << endl;
    cout << Foo(Foo(a)) << endl;


}
will not compile with g++ 3.2.2 and spits out the error
[tmp]$ g++ test.cpp
test.cpp: In function `int main()'':
test.cpp:14: could not convert `Foo(float&)((&a))'' to `float&''
test.cpp:5: in passing argument 1 of `float Foo(float&)''
It compiles fine with VS C++ 7. Am I doing something wrong here?

Share this post


Link to post
Share on other sites
Advertisement
Weird. Looks like gcc thinks you''re trying to pass a function pointer.

Looks like legal C++ to me.

Share this post


Link to post
Share on other sites
The value returned by foo is only temporarily valid.

For example: You won't be able to pass an intermediate value to Foo ( like: Foo(10) ), because a reference must 'refer' to an object, which it cannot as '10' isn't stored anywhere (It is for the runtime of the function, but after that it's invalid).

For the same reason you can't pass the result returned by foo to another function that expects a reference to a variable. The result of foo isn't stored anywhere, it's temporary. Thus you shouldn't be able to refer to it.

Offending line: Foo(Foo(a))

Edit: Maybe VC7 allows this because it actually 'sees' that you're not modifying the passed parameter, or maybe it has a different 'expiration date' of returned values... . Are you sure you did not return a float& in the VC code?

[edited by - Wildfire on August 19, 2003 3:58:20 AM]

Share this post


Link to post
Share on other sites
Btw. unless you''re gonna modify the passed parameter you should do the following:

float Foo(const float& input) {...}

The const is there to show the user of your function that whatever (s)he''s passing to you won''t get modified. Plus, using a const float& does allow you to pass intermediate values.

float Foo(const float& x) {return(x);}
float Bar(float& x) {return(x);}

...

Foo(10); // No Problem
Bar(10); // A reference that does not refer to a const value, may not refere to a non L-value
(liberaly tranlated from the german compiler message )

Share this post


Link to post
Share on other sites

This is a nonstandard extension that VC supports. GCC doesn''t support it because of religious reasons.

Share this post


Link to post
Share on other sites
Another thing. Unless you are changing the value of the parameter in the function (and want that value reflected in the caller), you don''t need to pass a reference for POD types (i.e. int, float, double, long, char, etc.)

In other words, "Foo(const float &f)" is completely unnecessary and you''re probably better off just doing "Foo(float f)."

Share this post


Link to post
Share on other sites
quote:
Original post by fizban75
you don''t need to pass a reference for POD types (i.e. int, float, double, long, char, etc.)



Your comment is misleading.

POD types include more than just built-in types. Classes (and structs and unions) without private, pointer, non-POD or virtual members are POD types too.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
What does POD stand for? Does his comment have any merit beyond the fact that those types are too small to gain much from passing by constant reference?

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
What is a POD-Type? [C++ FAQ Lite]

I guess he only wanted to point out that there's no actual gain when doing it with int, float etc... Maybe slower because char -> 8bit, char& -> 32bit, but does it matter on a 32bit machine?.

The real gain is with classes, since when passing by reference the computer won't have to create a copy of the class (constructor,destructor time-penalty) each time it's passed.

[edited by - Wildfire on August 19, 2003 10:34:45 AM]

Share this post


Link to post
Share on other sites
Yes, my comment was misleading because POD types are not restricted to the built-in types alone. So, I apologize and I will clarify my statement to say you don''t need to pass a const reference for built-in types such as int, float, char, pointers, etc.

Share this post


Link to post
Share on other sites

  • Advertisement