0

I am trying out template programming in C++ where I programmed a template for matrix.

template <
  typename T,
  unsigned int R, unsigned int C>
requires StringOrArithmeticType<T>
class matrix {
...
};

From the type traits I could constrain the T to floating point and integral types. How can I do it with for example a specific type like string?

template <typename T>
concept StringOrArithmeticType =
  is_integral_v<T> || is_floating_point_v<T> || is_string<T>::value;

So I implemented my own is_string. In type traits I could not find something helpful? Here I need some help, how should I solve this problem? Also, I would like to set the constrain that R and C must be greater than 1.

Code4Fun
  • 127
  • 4

1 Answers1

0

To check if the type is a string, use std::is_same_v<T, std::string>.

To constrain R and C, just add the appropriate conditions to the requires clause:

template<typename T, unsigned R, unsigned C>
requires (StringOrArithmeticType<T> && (R > 1) && (C > 1))
class matrix { ... };
Jonathan S.
  • 1,796
  • 5
  • 14
  • Great, thanks a lot. All the time I was looking for something like is_type or similar but it is is_same... :) – Code4Fun Feb 08 '23 at 14:00