2017-10-15 69 views
2

我試圖編譯C++庫(與海灣合作委員會5.3.1-14ubuntu2),並得到了這種類型的錯誤:明確違約功能不能被聲明爲constexpr因爲隱式聲明不constexpr

> 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

+0

它[作品](http://coliru.stacked-crooked.com/a/c32bbd2a1633014f)與GCC 7.2.0以及鏗鏘3.8.0。 – Darklighter

回答

1

這似乎是一個GCC的bug。假設您作爲編譯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接受。所以你肯定可以把它編成一個編譯器錯誤。

+0

所以你相信編譯GCC 5.4.0應該可以工作? –

+1

@ThomasCokelaer - 我的鏈接是一個在線編譯器,可以做到這一點。 – StoryTeller

+0

輝煌。我錯過了這個鏈接。我可以使用GCC 5.3.0重現錯誤。 –