How do I use concepts in if-constexpr?

  • A+

How does one use concepts in if constexpr?

Given the example below, what would one give to if constexpr to return 1 in case T meets the requirements of integral and else 0?

template<typename T> concept integral = std::is_integral_v<T>;  struct X{};  template<typename T> constexpr auto a () {     if constexpr (/* T is integral */) {         return 1;     }      else {         return 0;     } }  int main () {     return a<X>(); } 


Concepts are named boolean predicates on template parameters, evaluated at compile time.

In a constexpr if statement, the value of the condition must be a contextually converted constant expression of type bool.

So in this case, usage is simple:

if constexpr ( integral<T> ) 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: