11拒绝函数的constexpr限定条件

11拒绝函数的constexpr限定条件

本文介绍了为什么MSVC ++ 11拒绝函数的constexpr限定条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,使用constexpr玩弄,MSVC(Visual Studio 2012)给我一个错误,试图使用这个简单的程序(包括省略我的函数 constexpr ):

  constexpr int factorial(int n)
{
return n }

int main(void)
{
const int fact_three = factorial(3);
std :: cout<< fact_three<< std :: endl;
return 0;
}

constexpr 红色,并显示以下消息:

这真的让我很困惑,因为它是Cppreference 的使用。起初,我使用一个简单的函数返回一个字面值,即 constexpr int func(){return 5;} ,但是产生相同的错误。我将第一个消息解释为它应该是一个结构或类的成员函数,但是来自Cppreference的例子显示它不是显而易见的。



解决方案

很简单 - 因为Visual Studio不支持 constexpr (在之前)。 p>

注意MSVC ++ 11是Visual Studio 2012; VC ++ 10是Visual Studio 2010。


So, playing around with constexpr, MSVC (Visual Studio 2012) gave me an error while trying to qualify my function with the constexpr keyword using this simple program (includes omitted):

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;
}

constexpr was underlined red with the following message:

and trying to compile the program gave the following output:

It really puzzles me as it is the very example that Cppreference uses to illustrate the use of constexpr. At first I used a simple function that returned a literal, i.e. constexpr int func(){return 5;}, but which yielded the same error. I interpreted the first message as "it should be a member function of a struct or class", but the example from Cppreference shows that it's not necessary apparently.

So, what am I obviously missing here ?

解决方案

Quite simply - because Visual Studio doesn't support constexpr (prior to Visual Studio 2015).

Note that MSVC++11 is Visual Studio 2012; VC++10 is Visual Studio 2010.

这篇关于为什么MSVC ++ 11拒绝函数的constexpr限定条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:54