#Using Concepts to Check if a Type is Wrapped

13 messages · Page 1 of 1 (latest)

plucky glen
#

Say I have some template <typename T> struct K { ... };

I'd like to write a concept that accepts any type K<T> and rejects any type that is not "wrapped" by K.

Initially I was thinking template <typename T, V> concept Wrapped = std::same_as<T, K<V>> but I don't think template evaluator is smart enough to provide V automatically :(

still copperBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

plucky glen
#

bonus points if you don't use partial specializations for structs

indigo pilot
#

you can use overloading, or variable template partial specialization 😝

#

but I don't think there's gonna be a way to do that without any kind of specialization

sweet thicket
#
#include <vector>

template<class T>
struct K {};


template<class T>
struct is_wrapped
{
    static constexpr bool value = false;
};

template<class T>
struct is_wrapped<K<T>>
{
    static constexpr bool value = true;
};

template<class T>
concept Wrapped = is_wrapped<T>::value; 

int main()
{ 
    static_assert(Wrapped<K<int>>);
    static_assert(Wrapped<K<double>>);
    static_assert(!Wrapped<int>);
    static_assert(!Wrapped<std::vector<int>>);
}

No bonus points

indigo pilot
#

what for do you think you need that anyways?

still copperBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

plucky glen
#

Damn sorry I forgot :P

#

I appreciate it, I really just want the user to get an error if they give me a lambda who's invoke result isn't wrapped by K

#

so I want to requires is_wrapped<LambdaType>

#

K<T> is like an std::expected<T, Error>

still copperBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.