简体   繁体   中英

G++ flags to disable “temporary to non-const reference” error

I am looking for some command-line flags (should they exist) that disable the GCC error for this type of C++ code:

#include <string>

void m(std::string& s) { }

int main()
{
        m(std::string(""));
}

G++ gives this error:

error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'

The reason is to be able to quickly migrate from VC++ and Sun Studio (without any code changes), since both silently accept temporary to non-const lvalue ref conversions. I know what needs to be done in terms of code changes -- I am strictly asking about a way to do it without making code changes .

I will be using GCC 4.x.

Why should one want to disable an error ? Fix the code instead of relying on vendor extensions.

It is not conforming to the standard & there is no way to disable this through flags in GCC.

Vc++ wrongly supports this through an non-standard extension. Try with /Za (disable language extension) flag, and you should see the errors.

Or use can the /W4 flag to get maximum warnings, and it will show you:

warning C4239: nonstandard extension used

You could try to build your code with CLang.

There has been much work on CLang to get compatibility with VC++ source files (both the STL and the MFC code) and as a consequence CLang has the -fms-extensions flag to allow these extensions, and generate the appropriate code.

Most of MFC compile, so most of the extensions, including other oddities in templates, are covered too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM