2
votes

I'm trying to compile a C++ library (with gcc 5.3.1-14ubuntu2) and got this type of error:

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

1
It works with GCC 7.2.0 as well as Clang 3.8.0. - Darklighter

1 Answers

1
votes

This seems like a GCC bug. Assuming you compile as C++14, then the rules as written are these:

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

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.


This code (once the error inducing static functions are commented out), is accepted by GCC 5.4.0. So you can definitely chalk it up to a compiler bug.