简体   繁体   English

不能将显式默认函数声明为 constexpr,因为隐式声明不是 constexpr

[英]explicitly defaulted function cannot be declared as constexpr because the implicit declaration is not constexpr

I'm trying to compile a C++ library (with gcc 5.3.1-14ubuntu2) and got this type of error:我正在尝试编译一个 C++ 库(使用 gcc 5.3.1-14ubuntu2)并得到这种类型的错误:

> In file included from
> /root/pitchfork/workspace/unanimity/include/pacbio/consensus/ModelConfig.h:49:0,
>                  from /root/pitchfork/workspace/unanimity/src/models/P6C4NoCovModel.cpp:42:
> /root/pitchfork/workspace/unanimity/include/pacbio/data/internal/BaseEncoding.h:119:31:
> error: explicitly defaulted function 'constexpr
> PacBio::Data::detail::NCBI2na&
> PacBio::Data::detail::NCBI2na::operator=(const
> PacBio::Data::detail::NCBI2na&)' cannot be declared as constexpr
> because the implicit declaration is not constexpr:
>      inline constexpr NCBI2na& operator=(const NCBI2na&) = default;

The part of the code that cause trouble is :导致麻烦的代码部分是:

class NCBI2na
{
public:
    static inline constexpr NCBI2na FromASCII(const char base) { return NCBI2na{base}; }
    static inline constexpr NCBI2na FromRaw(const uint8_t raw) { return NCBI2na{raw}; }

public:
    ~NCBI2na() = default;

    inline constexpr NCBI2na(const NCBI2na&) = default;
    inline constexpr NCBI2na(NCBI2na&&) = default;

    inline constexpr NCBI2na& operator=(const NCBI2na&) = default;
    inline constexpr NCBI2na& operator=(NCBI2na&&) = default;    
};

The part of the code that seems to causes trouble is the "= default".似乎引起麻烦的代码部分是“= default”。 This may alos be related这也可能是相关的

I looked around but could not find a solution to this problem so far.我环顾四周,但到目前为止找不到解决此问题的方法。 Here are some similar questions that could help:以下是一些可以提供帮助的类似问题:

constexpr defining static data member of literal type that is declared const constructor of derived class cannot be constexpr if base class contains array member 如果基类包含数组成员,则定义被声明为派生类的const 构造函数的字面类型的静态数据成员的constexpr 不能是 constexpr

This seems like a GCC bug.这似乎是一个 GCC 错误。 Assuming you compile as C++14, then the rules as written are these:假设您编译为 C++14,那么编写的规则如下:

[dcl.constexpr]/3 [dcl.constexpr]/3

The definition of a constexpr function shall satisfy the following constraints: constexpr 函数的定义应满足以下约束:

  • it shall not be virtual它不应是虚拟的
  • its return type shall be a literal type;其返回类型应为文字类型;
  • each of its parameter types shall be a literal type;它的每个参数类型都应该是一个文字类型;
  • its function-body shall be = delete, = default, or ...它的函数体应该是 = delete, = default, or ...

All of the above are in fact satisfied in the code you've shown us.实际上,您向我们展示的代码满足了上述所有要求。 So your assignment operator definition is okay, and should be accepted as constexpr .所以你的赋值运算符定义没问题,应该被接受为constexpr


This code (once the error inducing static functions are commented out), is accepted by GCC 5.4.0 .这段代码(一旦引起静态函数的错误被注释掉),被GCC 5.4.0接受。 So you can definitely chalk it up to a compiler bug.所以你绝对可以把它归结为编译器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM