Home > other >  Is there something like std::unconvertible_to?
Is there something like std::unconvertible_to?

Time:10-23

I am trying to have a template parameter, that is allowed to be every type, except for one. And I have no clue how. I'm new to concepts, and don't fully understand them yet, but this is how I implemented std::convertible_to:

template <typename T>
    concept notSomeType = requires(T v)
{
    {v} -> std::convertible_to<SomeType>;
};

Is there anything like std::unconvertible_to built in?
If not, is there any other way to do this?

CodePudding user response:

You can simply create a concept that is a negation of std::convertible_to:

template<class From, class To>
concept NotSomeType = !std::convertible_to<From, To>;

template<NotSomeType<int> T>
void f(T)
{
    std::cout << "Not convertible to int\n";
}

template<std::convertible_to<int> T>
void f(T)
{
    std::cout << "convertible to int\n";
}

int main()
{
    f(1);
    f('a');
    f("Hello world");
    f([]() {});
}
  • Related