I have a template function that should work for all integral types:
#include <concepts>
template<typename T>
bool odd(T n) requires std::integral<T>
{
return n & T(1);
}
Now I want to use this function with some user defined integral type, e.g. boost::multiprecision::cpp_int
#include <boost/multiprecision/cpp_int.hpp>
boost::multiprecision::cpp_int n = ...
std::cout << odd(n) << std::endl;
gcc 10 gives
error: use of function ‘bool odd(T) requires integral<T> [with T = boost::multiprecision ...
which is, of course, correct. What can I do to fix this, i.e. how can I specialize a standard concept for my own type?
how can I specialize a standard concept for my own type?
You don't. Concepts can't be specialized. Type traits can be under certain circumstances, but std::integral
isn't one that you can, so that's not the right route either.
What can I do to fix this
You need to come up with the right concept for the set of algorithms you're using. Obviously the most literal one is:
template <typename T>
concept IsOddable = std::constructible_from<T, int> &&
requires (T n) {
{ n & n } -> std::convertible_to<bool>;
};
But don't do that. You probably need some kind of generic Numeric
concept and add requirements for, whatever, +
, *
, %
, etc. Concepts come from algorithms - so figure out the generic requirements that your algorithms are imposing and work up from there.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments