gpt4 book ai didi

c++ - constexpr void 函数被拒绝

转载 作者:IT老高 更新时间:2023-10-28 23:15:35 25 4
gpt4 key购买 nike

我有一个非常简单但无法编译的函数。

constexpr void func()
{
}

我得到的错误是:

error: invalid return type 'void' of constexpr function 'constexpr void func()'     constexpr void func()

在 C++14 中,void 是文字类型 [§3.9/10]:

A type is a literal type if it is:

  • void; or
  • a scalar type; or
  • a reference type; or
  • an array of literal type; or
  • a class type (Clause 9) that has all of the following properties:
    • it has a trivial destructor,
    • it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
    • all of its non-static data members and base classes are of non-volatile literal types.

有人可以解释为什么这是无效的吗?

最佳答案

使 void 成为文字类型的提案是 n3652 Relaxing constraints on constexpr functions . G++ 决定将此功能推送到版本 5 (我使用的是 4.9.2):

G++ now supports C++14 extended constexpr.

constexpr int f (int i)
{
int j = 0;
for (; i > 0; --i)
++j;
return j;
}

constexpr int i = f(42); // i is 42

Clang 从 3.4 版开始实现了这一点。

关于c++ - constexpr void 函数被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com