开发者

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 chang开发者_开发技巧es.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜