-2

Why is the concept std::integral defined as

template < class T > concept integral = std::is_integral_v<T>;

and not as

template < class T > concept integral = std::numeric_limits<T>::is_integer;

?

This means that the concept std::integral is restricted to "fundamental integral". For example, this is true:

std::numeric_limits<boost::multiprecision::cpp_int>::is_integer;

but this is false:

std::integral<boost::multiprecision::cpp_int>;

Is there any option to activate std::integral for user-defined types? What is missing e.g. in boost::multiprecision::cpp_int so that it does not fulfill the concept integral?

6
  • 5
    Use a different concept that does what you want. Commented Aug 29, 2021 at 17:14
  • Yes. This is exactly the problem. I always had to use my own concept and never could use std::integral. But I guess there must be a convincing example where std::integral is appropriate. Commented Aug 29, 2021 at 18:07
  • std::integral implies some things that std::numeric_limits<...>::is_integer doesn't, like triviality, etc. By hacking it to be true for user-defined types, you'd break those assumptions. Commented Aug 29, 2021 at 18:25
  • Give me an example where you need these additional properties. Commented Aug 29, 2021 at 19:39
  • Anything that needs or expects trivality, e.g. memcpying the numbers. Also implicitly starting their lifetime. Commented Aug 29, 2021 at 20:22

1 Answer 1

1

This means that the concept "std::integral" is restricted to "fundamental integral".

Yes; that's what std::integral is for: telling you if a type is an integral type as defined by the standard.

If you want "integer type as defined by numeric_limits" or something else, you're going to need a different definition and therefore a different concept.

Sign up to request clarification or add additional context in comments.

8 Comments

Do you have an example where std::integral is appropriate?
@HelmutZeisel: I don't understand. It's appropriate when you want/need to take an integral type. Maybe you're using that type as the type of a non-type template parameter. Maybe you're using it as the type for an enum's underlying type. What matters is what question you want to ask.
Have you ever used std::integral? How often?
@HelmutZeisel: Nonsense. Not unless you're claiming that people would be using your definition for integral were it available. Is it not more likely that it's just not very common for people to write templates that have to take integers for some purpose? Just because you personally don't have a use for it doesn't mean it shouldn't be a concept.
@HelmutZeisel: Such a question is not permitted here. What you're asking is more like a survey, not a request for useful information. And we tend to prefer questions that are about useful information.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.