Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can a concept satisfaction of an expression, contains both type and the reference?

Is there a way to make the following code not so bloated?

I mean join both type and a reference somehow (|| does not work).

template<typename T>
concept IntegralVector = std::integral<typename T::value_type> &&
requires(T t)
{
    { t.size() } -> std::convertible_to<std::size_t>;
}
&& (requires(T t)
{
    { t[0] } -> std::same_as<typename T::value_type&>;
} || requires(T t)
{
    { t[0] } -> std::same_as<typename T::value_type>;
});

A working trick can be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{ 0 + t[0] } -> std::integral;

But I want to stick with typename T::value_type

>Solution :

You probably want something like this:

template <typename T, typename U>
concept decays_to = std::same_as<std::decay_t<T>, U>;

To use as:

template<typename T>
concept IntegralVector =
    std::integral<typename T::value_type>
    && requires (T t) {
        { t.size() } -> std::convertible_to<std::size_t>;
        { t[0] } -> decays_to<typename T::value_type>;
    };

This also catches value_type const& as an option, which I’m not sure was intentionally omitted.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading