gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:07:53 26 4
gpt4 key购买 nike

我正在尝试编译 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;

导致问题的代码部分是:

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;
};

代码中似乎有问题的部分是“= default”。这也可能是相关的

我环顾四周,但到目前为止找不到解决这个问题的办法。以下是一些可能有所帮助的类似问题:

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

最佳答案

这似乎是一个 GCC 错误。假设您编译为 C++14,那么编写的规则如下:

[dcl.constexpr]/3

The definition of a constexpr function shall satisfy the following constraints:

  • 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 ...

您向我们展示的代码实际上满足了以上所有条件。所以你的赋值运算符定义没问题,应该被接受为 constexpr


此代码(一旦错误诱导静态函数被注释掉),被 GCC 5.4.0 接受.因此,您绝对可以将其归因于编译器错误。

关于c++ - 显式默认函数不能声明为 constexpr,因为隐式声明不是 constexpr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46756288/

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