Conversion to reference

Started by
10 comments, last by sQuid 20 years, 8 months ago
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?
Advertisement
Weird. Looks like gcc thinks you''re trying to pass a function pointer.

Looks like legal C++ to me.
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]
How do I set my laser printer on stun?
Try
float Foo(const float& arg) {    return arg;} 

instead.

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 )
How do I set my laser printer on stun?

This is a nonstandard extension that VC supports. GCC doesn''t support it because of religious reasons.
OK, that clears things up a bit.

Thanks people.
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)."
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement