Paulo Coutinho
Paulo Coutinho

Reputation: 705

G++ and STD 11 has problems with constexpr

Im trying use the same constexpr in g++ and clang++, both latest version and with param "-std=c++11". Clang compiles without problem, but G++ return error. The source is:

#include <functional>

enum class LoggerLevel : unsigned {
    NO_LEVEL = 0,
    VERBOSE = 1 << 0,
    DEBUG = 1 << 1,
    INFO = 1 << 2,
    WARNING = 1 << 3,
    ERROR = 1 << 4,
    FATAL = 1 << 5,
    ALL_LEVELS = 0 | VERBOSE | DEBUG | INFO | WARNING | ERROR | FATAL,
};

constexpr LoggerLevel operator|(LoggerLevel lhs, LoggerLevel rhs) noexcept {
    return static_cast<LoggerLevel>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
}

constexpr LoggerLevel& operator|=(LoggerLevel& lhs, LoggerLevel rhs) noexcept {
    return lhs = lhs | rhs;
}

int main()
{
    auto x = LoggerLevel::ALL_LEVELS;
    return 0;
}

And the error is:

<source>: In function 'constexpr LoggerLevel& operator|=(LoggerLevel&, LoggerLevel)':

<source>:19:16: error: expression '(lhs = operator|(lhs, rhs))' is not a constant expression

     return lhs = lhs | rhs;

            ~~~~^~~~~~~~~~~

Compiler returned: 1

And the godbolt example:

https://godbolt.org/z/M6ERms

Thanks with any help.

Upvotes: 2

Views: 235

Answers (1)

Ron
Ron

Reputation: 15511

Compile for at least the C++14 standard as prior to C++14, the core constant expression evaluation would not evaluate an assignment or a compound assignment operator which is what you have in:

return lhs = lhs | rhs;

The relevant chapter in the C++11 draft is 5.19.2 Constant expressions (emphasis mine):

A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression ...

  • an assignment or a compound assignment

Upvotes: 5

Related Questions