How to compile Visual Studio C ++ 2010 How is gcc / g ++? (or vice versa)

Let's say you have the following simple main.cpp file:

#include <cstdlib> #include <iostream> #include <fstream> using namespace std; int main() { const string FILENAME = "foo.txt"; ifstream somefile(FILENAME); populations.close(); return 0; } 

This compiles using Visual Studio C ++ 2010.

However, on a Linux-based system, if I execute make main and compile, we get the expected error, since we did not call c_str() in a string constant, for example:

 ifstream somefile(FILENAME.c_str()); 

As is well known and described in this SO stream .

How can I make VS behave like gcc / g ++ and raise a compilation error for the code above? Or, how can I get gcc / g ++ to behave like VS and compile above without errors? (Is it just a matter of updating my gnu compiler?)

(I don’t think that disabling compiler extensions is a solution, as I did, and it still compiles without errors.)

+6
source share
2 answers

It is available as part of the new C ++ standard.

To disable, add

  #define _HAS_CPP0X 0 

at the top before you turn it on.

+5
source

Visual Studio behaves correctly in this case according to the C ++ 11 standard ( it now works in g ++ too ). I'm not sure why you want to do this, but you will probably have to edit the MSVC headers (not recommended and quite radical).

It is strange, however, that they do not record it in their documentation . Can you check which constructor is actually being called?

+6
source

Source: https://habr.com/ru/post/898842/


All Articles