C++ constexpr 未在 VC2013 中编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20264644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:00:23  来源:igfitidea点击:

constexpr not compiling in VC2013

c++c++11visual-studio-2013constexpr

提问by Damian

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

此 constexpr 代码未在 Visual Studio 2013 版本 12.0.21005.1 REL 中编译

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

是否有适用于 constexpr 的较新的 Visual Studio 编译器?

#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

Herb Sutter 在他的博客上提到了 constexpr,但不清楚它在哪个版本中有效/将有效?http://herbsutter.com/2013/09/09/visual-studio-2013-rc-is-now-available/#comment-13521

回答by Lightness Races in Orbit

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

微软发布一个C ++ 11兼容性表,在其下constexpr清楚地标记为不是在Visual Studio 2013可用

The November 2013 CTPhas it, though.

不过,2013 年 11 月的 CTP有它。

Source:Google visual studio constexpr

资料来源:谷歌visual studio constexpr

回答by Werner Henze

constexpris 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.

constexprVisual Studio 2013 RTM 不支持,请参阅兼容性表。这不仅适用于 RTM 版本,也适用于 Visual Studio 更新。

If you want to stick to Visual Studio 2013, you could download the Visual C++ Compiler November 2013 CTPwhich 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.

如果您想坚持使用 Visual Studio 2013,您可以下载带有一些新功能的Visual C++ Compiler 2013 年 11 月 CTP,请参阅MSDN 博客。不幸的是,Microsoft 没有与最新的 Visual Studio 更新功能和 CTP 功能合并,并明确表示他们不打算这样做。

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

如果我们想要这一切,我们需要等待 Visual Studio 2015,请参阅有关 VS 2015 PreviewMSDN 博客

回答by Jonathan Mee

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

正如其他人所提到的,2013 年 11 月的客户技术预览 (CTP)将使您能够访问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".

请注意,只需下载您需要将“平台工具集”更改为“Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013)”才能使用新编译器。您可以通过打开项目的“属性页面”并转到:“配置属性”>“常规”然后更改“平台工具集”来实现。

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

*关于constexpr您实际可以访问的部分信息存在一些相互矛盾的信息,但这绝对不是constexpr. 微软在这里说 2013 年 11 月的 CTP 增加了:

constexprsupport (except for constructors)

constexpr支持(构造函数除外)

Microsoft say herethat it contains:

微软在这里说它包含:

constexpr(except for member functions)

constexpr(成员函数除外)

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

我什至无法测试它是否支持成员函数,因为它绝对不支持任何类型的constexpr构造。例如,此代码在 2013 年 11 月的 CTP 中出现此错误:

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

错误 C2127:使用非常量表达式非法初始化“constexpr”实体

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

附加说明:在编写 Visual Studio 2015 预览版时,仍不支持constexpr构造。在最终版本中保持我的手指交叉。

回答by Abei Villafane

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

您需要安装 VS2013 更新 5。(我在更新 3 上,但它不起作用)在撰写本文时,有关“2013 年 11 月 CTP”的内容不适用。您可以访问这里:https: //my.visualstudio.com

and going to download, or : https://my.visualstudio.com/Downloads?q=visual%20studio%202013&wt.mc_id=o~msft~vscom~older-downloads

然后去下载,或者:https: //my.visualstudio.com/Downloads?q=visual%20studio%202013&wt.mc_id=o~msft~vscom~older-downloads