18

I am currently using Visual Studio Community 2017. From looking at the C++ Language Standards in the project properties, they only provide C++14 and C++17. Since my code was completed for a previous assignment using a compiler for C++11, I am unable to run my code using functions such as stoi. My question is if there is a way to add C++11 to the language standards for C++?

I am creating a DLL for a GUI, my initializations are:

#include <string>
#include "stdafx.h"

using namespace std;

Here I am creating a fraction class, the main errors follow in the ifstream:

istream& operator>>(istream& in, Fraction& f) {

string number;
in >> number;                           //read the number

size_t delimiter = number.find("/");    //find the delimiter in the string "/"

if (delimiter != string::npos) {            //if delimiter is not empty

    int n = stoi(number.substr(0, delimiter));      //set numerator from string to integer before the "/"
    int d = stoi(number.substr(delimiter + 1));     //set denominator from string to integer after the "/"

    if (d == 0) { //if denominator is 0
        throw FractionException("Illegal denominator, cannot divide by zero.");  //illegal argument throw
    }
    else if (n == 0 && d != 0) {    //numerator is 0, then set values as zero fraction
        f.numVal = 0;
        f.denVal = 1;
    }
    else {                      //set the values into the fraction and normalize and reduce fraction to minimum
        f.numVal = n;
        f.denVal = d;

        f.normalizeAndReduce(f.numVal, f.denVal);
    }
}
else {  //else if there is no delimiter it would be a single integer
    f.numVal = stoi(number);
    f.denVal = 1;
}

return in;
}

I am getting the following errors:

C2679: binary '>>': no operator found which takes a right-hand operator of type 'std::string"
C3861: 'stoi' identifier not found

This method worked perfectly fine in eclipse, not sure what I am doing wrong.

6
  • C++11 is default (that is there is no flag to disable implemented C++11 features). See Visual C++ Language Conformance. Commented Oct 31, 2017 at 20:22
  • 2
    VS2017 is almost fully compliant with C++11, including stoi (learn.microsoft.com/en-us/cpp/standard-library/…) maybe if you show the problem code we could help.
    – zdan
    Commented Oct 31, 2017 at 20:33
  • @zdan I added the problem code, thanks for the help! Commented Oct 31, 2017 at 20:45
  • 2
    You forgot to #include both <string> and <iostream>, haven't you? Commented Oct 31, 2017 at 20:46
  • @sam the iostream is in my header file and it is included in the "stdafx.h" Commented Oct 31, 2017 at 20:46

4 Answers 4

33

The Visual C++ 2017 compiler is C++11/C++14 compliant with a few specific exceptions:

  • Expression SFINAE is implemented, but not complete. (Now complete in VS 2017 (15.7))
  • Full C99 preprocessor support is limited due to some bugs with variadic macros
  • Two phase name lookup is in VS 2017 (15.3 update) but is incomplete and only active when using /permissive- (Now complete in VS 2017 (15.7))

The compiler does not offer a specific C++11 mode and defaults to C++14, but that standard is fully inclusive of C++11. C++17 support is in progress, and requires you use the /std:c++17 or /std::c++latest switch.

std::stoi requires you include the appropriate header, specifically <string>> Either you forgot to include that header -or- you didn't deal with the namespace resolution (either explicitly as std:: or via using namespace std;)

See C++17 Features And STL Fixes In VS 2017 15.3 for the latest status of C++11/C++14/C++17 standards conformance as of the VS 2017 (15.3 update)

UPDATED: For the latest on Visual C++ conformance, see Microsoft Docs.

Now that you have posted your code, I see that the problem has nothing to do with which standard is supported. Your problem is that you don't know the secrets of how Precompiled Headers work.

Change:

#include <string>
#include "stdafx.h"

to:

#include "stdafx.h"
#include <string>

-or- add #include <string> to the precompiled header stdafx.h directly.

See Creating Precompiled Header Files

11
  • Compiler defaults to C++11 (actually it was a bit messy until switches were introduced). In order to use C++14 /std:c++14 flag should be used. See Standards version switches in the compiler. Commented Oct 31, 2017 at 20:30
  • 3
    @VTT The Visual C++ team didn't feel there was much value in having a distinct C++14 vs. C++11 mode. C++14 is really just some bug fixes for C++11 plus a few improvements to make use of C++11 constructs more general. In addition to enabling new C++17 constructs, the /std::c++17 switch also has the effect of removing some Standard Library things that were deprecated in C++14 so it's potentially a bigger impact on code. Also, a few C++14 things were already in VS 2013 so it wasn't quite as a cleanly separated from C++11. Commented Oct 31, 2017 at 20:37
  • 1
    @user3344484: you should post a question specifically about your stoi() problem. including a minimal, complete example that still causes the problem and include the compiler command line use to build the program (and produces the error). Note that even when using the IDE to build you can find the command line used to compile in the "CL.command.1.tlog" file in the build's output directory. Commented Oct 31, 2017 at 20:44
  • 2
    Yup. When precompiled headers are being used, anything before the PCH headers is included is ignored in all the source file except the one that's used to generate the PCH (which is stdafx.cpp by default in the VS templates). It was like your #include <string> wasn't there... Another fix here would have been to disable PCH usage for the entire project or even just your one source file. Commented Oct 31, 2017 at 21:00
  • 1
    Precompiled headers in the Win32 console app template is indeed silly. It does, however make a lot of difference for Win32 windows apps because #include <windows.h> alone pulls in a lot of stuff. Commented Oct 31, 2017 at 21:06
4

Microsoft I think has declared that

Note that there is no plan to add a C++11 switch. link

So there's not an explicit switch

0

As an update to this, VS 2017, Update 9.4 (releasd Dec 2018) is now fully C++17 compliant.

1
  • 1
    OP's question is regarding C++11 compliance.
    – chb
    Commented Feb 28, 2019 at 14:03
-7

C++ is an evolving standard - after 2003 there was 2011 (C++11) then 2014 (C++14) and now we have 2017 (C++17) and we're working towards 2020 (C++20). They're upward compatible.

Look here for Microsoft Visual C++ support for the various standards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.