C++ concept equivalent of std::decay?

Viewed 85

I'm new to concepts. As far as I understand it the concept library lists all the available std concepts. Yet there seems nothing like std::decay? Compare the following general use case where I want to restrict the input to a method to the class specialization type:

Demo

#include <type_traits>
#include <concepts>

template <typename T>
struct some_struct
{
    template <typename U, typename = std::enable_if_t<std::is_same_v<std::decay_t<U>, T>>>
    void do_something(U obj) {
        return ;
    }
};

int main()
{
    some_struct<int> obj;

    obj.do_something(0);
}

How can I accomplish this with concepts?

1 Answers

You could add a concept like:

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

Which would allow:

template <DecaysTo<T> U>
void do_something(U&&);

Note that this only really makes sense when using a forwarding reference. In the original example where the parameter type is U, std::decay_t<U> is just U, so DecaysTo<T> reduces to std::same_as<T>. It still correct, just unnecessarily involved.

Related