sách gpt4 ăn đã đi

c++ - 为什么 MSVC++11 拒绝函数的 constexpr 限定?

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 12:31:13 34 4
mua khóa gpt4 giày nike

因此,在使用 constexpr 时,MSVC (Visual Studio 2012) 在尝试使用这个简单的程序(包括省略)使用 constexpr 关键字限定我的函数时给了我一个错误:

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;
trả về 0;
}

constexpr 带有红色下划线,并带有以下消息:

Error : this declaration has no storage class or type specifier

并尝试编译程序给出以下输出:

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

这真的让我很困惑,因为它正是 Cppreference uses to illustrate the use of constexpr 的例子。 .起初,我使用了一个返回文字的简单函数,即 constexpr int func(){return 5;},但产生了相同的错误。我将第一条消息解释为“它应该是结构或类的成员函数”,但来自 Cppreference 的示例表明它显然没有必要。

那么,我显然在这里遗漏了什么?

câu trả lời hay nhất

很简单——因为 Visual Studio 不支持 constexpr(在 Visual Studio 2015 之前)。

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

关于c++ - 为什么 MSVC++11 拒绝函数的 constexpr 限定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513618/

34 4 0
Cựu chiến binh CNTT
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress