constexpr not compiling in VC2013

35,023

Solution 1

Microsoft publishes a C++11 compatibility table, under which constexpr is clearly marked as not being available in Visual Studio 2013.

The November 2013 CTP has it, though.

Source: Google visual studio constexpr

Solution 2

constexpr is not supported in Visual Studio 2013 RTM, see the compatibility table. This is not only true for the RTM version, but also for the Visual Studio Updates.

If you want to stick to Visual Studio 2013, you could download the Visual C++ Compiler November 2013 CTP which comes with some new features, see MSDN blog. Unfortunately Microsoft has no merger with the latest Visual Studio Update features and the CTP features and clearly states that they don't plan to do so.

If we want it all, we need to wait for Visual Studio 2015, see the MSDN blog about VS 2015 Preview.

Solution 3

As is mentioned by the others, November 2013 Customer Technology Preview(CTP) will give you access to constexpr*

Note that just downloading the you'll need to change your "Platform Toolset" to "Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013)" to use the new compiler. You can do that by opening your project's "Property Pages" And going to: "Configuration Properties" > "General" and then changing the "Platform Toolset".

*There is a bit of conflicting information on what portion of constexpr you actually have access to, but it's definitely not all of the standards definition of constexpr. Microsoft says here that the November 2013 CTP adds:

constexpr support (except for constructors)

Microsoft say here that it contains:

constexpr (except for member functions)

I can't even test if it has support for member functions, cause it definitely doesn't have support for any type of constexpr construction. For example this code gives this error with the November 2013 CTP:

error C2127: illegal initialization of 'constexpr' entity with a non-constant expression

One additional note: At time of writing the Visual Studio 2015 Preview still does not support constexpr construction. Keeping my fingers crossed on the final release.

Solution 4

You need to install VS2013 Update 5. (I was on Update 3 and it was not working) The thing about "Nov 2013 CTP" was inapplicable, as of this writing. You can do so by going here: https://my.visualstudio.com

and going to download, or : https://my.visualstudio.com/Downloads?q=visual%20studio%202013

Share:
35,023

Related videos on Youtube

Damian
Author by

Damian

I love "C with Classes" as in C++

Updated on September 19, 2020

Comments

  • Damian
    Damian over 3 years

    This constexpr code does not compiled in Visual Studio 2013 version 12.0.21005.1 REL

    Is there a newer Visual Studio compiler that works with constexpr?

    #include <iostream>
    
    constexpr int factorial(int n)
    {
        return n <= 1 ? 1 : (n * factorial(n - 1));
    }
    
    int main(void)
    {
        const int fact_three = factorial(3);
        std::cout << fact_three << std::endl;
        return 0;
    }
    

    output from compilation:

        1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
        1>  Source.cpp
        1>....\source.cpp(3): error C2144: syntax error : 'int' should be preceded by ';'
        1>....\source.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
        ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Herb Sutter mentions constexpr on his blog but is unclear in what version it works / will work? http://herbsutter.com/2013/09/09/visual-studio-2013-rc-is-now-available/#comment-13521

    • Jesse Good
      Jesse Good over 10 years
      You need to get 2013 CTP, although note its still not complete (not yet implemented for member functions)).
    • RichardPlunkett
      RichardPlunkett over 10 years
      compiled and ran fine on gcc version 4.8.2 (GCC) for cygwin, with --std=c++11
    • Damian
      Damian over 10 years
      Yes 2013 CTP works like a charm! ;-)
    • Lightness Races in Orbit
      Lightness Races in Orbit over 10 years
    • Ray
      Ray over 9 years
      What does the CTP stand for?
    • Jonathan Mee
      Jonathan Mee about 9 years
  • blfuentes
    blfuentes over 9 years
    I have VS 2k13 Update 3 but not this compiler. I had to install it. Do you know if they will release it with newer updates or we have to wait until vs14?
  • Lightness Races in Orbit
    Lightness Races in Orbit over 9 years
    @blacai: I don't know. Why not try the November 2013 CTP?
  • blfuentes
    blfuentes over 9 years
    That is what I did. But I was checking if it was planned to update the compiler in a major update. The nov13 ctp was released last year and vs2k13 got several updates in 2014
  • Lightness Races in Orbit
    Lightness Races in Orbit over 9 years
    @blacai: Can't help you there. The compatibility table says it's still unsupported and MS aren't great at documentation.
  • Jean Davy
    Jean Davy over 9 years
    "it's still unsupported" in this CTP, check comment from 'Lightness Races in Orbit' above ...
  • Werner Henze
    Werner Henze over 9 years
    @Jean Davy: Sorry, but this is nonsense. I just compiled the code from the question with the linked CTP.
  • ildjarn
    ildjarn over 9 years
    If you're going to update your answer, you should take out any references/links to the CTP now that the VS2015 preview is available.
  • Werner Henze
    Werner Henze over 9 years
    @ildjarn The MS Download page says "VS2015 Preview is not intended for production use." I did not see such a statement for the November CTP download.
  • ildjarn
    ildjarn over 9 years
    @WernerHenze : All MSFT CTPs (previews ;-]) have such wording.
  • rubenvb
    rubenvb over 9 years
    @Werner "This is a Customer Technology Preview and does not come with a "Go Live" license." Sounds like its not recommended for production use either. The name kinda says it all, no?
  • TankorSmash
    TankorSmash over 8 years
    So the CTP is a preview, so it means it's not truly supported, and we shouldn't be rushing to use it. Treat it more like an experimental release is what I'm asking? Nevermind, down below there's a link saying not to use it in production.
  • Lightness Races in Orbit
    Lightness Races in Orbit over 8 years
    @TankorSmash: Bear in mind that was two years ago. We have proper Visual Studio 2015 now, which the same linked table marks as having constexpr support.
  • Emil Styrke
    Emil Styrke over 6 years
    VS2013 Update 5 does not include support for constexpr, as far as I can tell (I get a syntax error when compiling).