Type conversion at template non-type argument without constexpr

前端 未结 3 1533
天涯浪人
天涯浪人 2021-01-13 04:55

Consider the following code:

struct A {
    constexpr operator int() { return 42; }
};

template 
void foo() {}

void bar(A a) {
    foo(         


        
3条回答
  •  执念已碎
    2021-01-13 05:21

    The user-defined conversion is allowed by [expr.const]/(4.1), and I don't see a single applicable bullet point in [expr.const]/2 that would prevent your expression from being a constant one. In fact, the requirements are so loose that declaring a as

    A a;
    

    is still giving a well-formed program, even if a didn't have a constexpr default constructor etc., since the conversion operator is constexpr and no members are evaluated.

    As you saw yourself, GCC is contradictory in that it allows a in the static_assert condition but not a template-argument.

提交回复
热议问题