   Conversion to reference
  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.cpptest.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?  
  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];  Tryfloat 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 ProblemBar(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 ) ;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 ] ;  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______________________________________________________________ 