Advertisement

Another g++ compiler error

Started by June 06, 2005 09:07 PM
1 comment, last by shrtysk8r1 19 years, 5 months ago
Please bare with me, as i have been converting my application to linux and run into a few differences between MSVC and g++ compilers. The following code produces an error in g++ and not in MSVC:

void foo(Class& c)
{
  (...)
}

int bar(void)
{
   foo(Class(param1, param2));
}
The error is that the compiler is unable to convert from type c to c&. When i convert the code to the following - everything works as planned, however i would like to avoid the extra variable and line of code.

int bar(void)
{
    Class temp(param1, param2);
    foo(temp);
}
If anyone is able to provide some insight into my "problem" it would be greatly appreciated. [Edited by - shrtysk8r1 on June 6, 2005 9:33:58 PM]
I'm assuming you meant "Class" not "c" as argument for the call to foo.

The later versions of GCC claim that its behavior is standards compliant -- you can't have a non-const reference to a temporary object. Microsoft, meanwhile, appears to believe that that's OK. Just re-formulate the code like the latter, and it'll work in both cases.

You get into similar problems when nuking STL containers:

  std::container<SomeType> myContainer;  myContainer.swap( std::container<SomeType>() ); // doesn't work on GCC  std::container<SomeType>().swap( myContainer ); // works on both

enum Bool { True, False, FileNotFound };
Advertisement
Yes, sorry for the typo -- I just recently found that the reference had to be const for that to work, so i guess there's no way of getting around the extra line, thanks for your help.

This topic is closed to new replies.

Advertisement