Type conversion at template non-type argument without constexpr

前端 未结 3 1569
天涯浪人
天涯浪人 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:27

    As @Jarod42 suggests a should be constexpr. This is because templates are deduced at compile time, and so non-type arguments also have to be available at compile time. constexpr is a promise that they will be available at compile time.

提交回复
热议问题