为什么MSVC ++ 11拒绝函数的constexpr限定条件? [英] Why does MSVC++11 rejects constexpr qualification of a function?

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

问题描述

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

  constexpr int factorial(int n)
{
return n <= 1? 1:(n *阶乘(n-1));
}

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

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


错误:此声明没有存储类或类型说明符




并尝试编译程序,得到以下输出:


1> main.cpp (5):错误C2144:语法错误:'int'应该在前面加上';'



1> main.cpp(5):error C4430: - int假设。注意:C ++不支持default-int


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



解决方案

很简单 - 因为Visual Studio不支持 constexpr (在 Visual Studio 2015 之前)。 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:

Error : this declaration has no storage class or type specifier

and trying to compile the program gave the following output:

1>main.cpp(5): error C2144: syntax error : 'int' should be preceded by ';'

1>main.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

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限定条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆