constexpr void函数被拒绝 [英] constexpr void function rejected

查看:390
本文介绍了constexpr void函数被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的函数,不会编译.

I have this very simple function which won't compile.

constexpr void func()
{
}

我得到的错误是:

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

     constexpr void func()

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

In C++14, void is a literal type [§3.9/10]:

如果是,则类型是文字类型:

  • void;或
  • 标量类型;或
  • 参考类型;或
  • 文字类型的数组;或
  • 具有以下所有属性的类类型(第9条):
    • 它有一个琐碎的析构函数,
    • 它是聚合类型(8.5.1)或具有至少一个不是复制或移动构造函数的constexpr构造函数或构造函数模板,并且
    • 其所有非静态数据成员和基类均为非易失性文字类型.
    • 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.

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

      Can someone explain why this is invalid?

      推荐答案

      使void成为文字类型的提案是 n3652对constexpr函数的放宽约束 . G ++决定将此功能推至版本 5 (我使用的是4.9.2):

      The proposal which made void a literal type was n3652 Relaxing constraints on constexpr functions. G++ decided to push this feature to version 5 (I was using 4.9.2):

      G ++现在支持C ++ 14扩展的constexpr.

      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
      

      从3.4版开始,Clang就实现了这一点.

      Clang has had this implemented since version 3.4.

      这篇关于constexpr void函数被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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