Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make Visual Studio C++ 2010 Compilation Behave Like gcc/g++? (or vice-versa)

Say you've got 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 fine via Visual Studio C++ 2010.

However, on a Linux-based system, if I execute make main and compile, we get an expected error since we didn't call c_str() on the string constant, like so:

ifstream somefile(FILENAME.c_str());

As is commonly known, and described in this SO thread.

How can I get VS to 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 the above without error? (Is it a simple matter of upgrading my gnu compiler?)

(I don't believe disabling compiler extensions is a solution, as I've done this and it still compiles without error.)

like image 932
ybakos Avatar asked Oct 07 '11 15:10

ybakos


2 Answers

Visual Studio behaves correctly in this case with respect to the C++11 standard (it works on g++ now, too). I'm not sure why would you want to do this, but you'll probably need to edit MSVC's headers (not advisable and rather drastic).

Strange thing is though, that they don't write it in their documentation. Can you check which constructor is actually being called?

like image 87
jpalecek Avatar answered Oct 26 '22 23:10

jpalecek


It is available as part of the newer c++ standard.

To disable, add

  #define _HAS_CPP0X 0

at the top before your includes.

like image 45
crashmstr Avatar answered Oct 26 '22 23:10

crashmstr