How can I specialize a standard concept for my own class?

Helmut Zeisel

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?

Barry

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make my own timer without standard library?

How can I use my own external class in CakePHP 3.0?

how can I list-initialize my own class?

How can I pass my class into its own constructor?

How can I specialize a class for enums of underlying type int?

How can I partially specialize a existing template class into a new type?

How can I specialize a operator function type by class template

How can I partially specialize a class template for enums values?

Java - How can I convert an object from my own class to a string in the main class?

How can I return my own promise?

How can I host my own website

How can I customize my own Observable?

How can I generate my own ScalaSig?

how can I spider my own website

in a python attrs class, how can I override generated __init__ with my own

how i can show my own active class on items in owl slider by click

How can I use the Xamarin.Forms.Setter class in my own ContentView (custom control)?

How can I partially specialize class template non-type parameters

How can I specialize a class of a namespace and use it without modifying too much code?

How can I add App Indexing concept in my Application?

Why can I not access UmbracoHelper in my own class

how can I use recursive concept with a relational class?

Can I specialize an variadic template template parameter with non template class?

How do I define my own StepActions class in a Karate test?

AS3: How do i draw shapes in my own class

Can I override a macro from the standard library with my own custom macro?

How to use my own version of a standard package

How do i specialize my template for std::string

How can I mprove my code so that it can be more standard?