Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

G++ flags to disable "temporary to non-const reference" error

Tags:

c++

gcc

g++

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.

like image 462
Nick Avatar asked Jan 19 '23 11:01

Nick


1 Answers

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

like image 76
Sebastian Mach Avatar answered Jan 29 '23 07:01

Sebastian Mach