How to use concept to restrict typename to numeric? C++20 features

Viewed 574

I am learning some modern C++20 features. One of them is concept. I have read https://en.cppreference.com/w/cpp/language/constraints and https://www.modernescpp.com/index.php/defintion-of-concepts to get some examples to follow.

For now, I want to design a concept such that I can only accept numeric data type. In the traditional method, one can use

template<typename NumericType,
        typename = typename std::enable_if<std::is_arithmetic<NumericType>::value,NumericType>::type>

as suggested in Class template for numeric types

or one may also use static_assert() inside the definition of template class/function

static_assert(std::is_arithmetic<NumericType>::value, "NumericType must be numeric");

I am wondering what the syntax for concept should be? For now, I am doing

template<typename NumericType>
concept Numeric = std::is_arithmetic<NumericType>::value ;

and find that

template<Numeric T>
void f(T){}

can do the trick I expected (basically just duck typing). I am wondering am I correct or is there any difference?

0 Answers
Related