Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFINAE: "enable_if cannot be used to disable this declaration"

Why can I not use enable_if in the following context?

I'd like to detect whether my templated object has the member function notify_exit

template <typename Queue>
class MyQueue
{
   public:
    auto notify_exit() -> typename std::enable_if<
            has_member_function_notify_exit<Queue, void>::value,
            void
        >::type;

    Queue queue_a;
};

Initialised with:

MyQueue<std::queue<int>> queue_a;

I keep getting (clang 6):

example.cpp:33:17: error: failed requirement 'has_member_function_notify_exit<queue<int, deque<int, allocator<int> > >, void>::value';
      'enable_if' cannot be used to disable this declaration
            has_member_function_notify_exit<Queue, void>::value,

or (g++ 5.4):

In instantiation of 'class MyQueue<std::queue<int> >':
33:35:   required from here
22:14: error: no type named 'type' in 'struct std::enable_if<false, void>'

I've tried a bunch of different things, but can't figure out why I can't use enable_if to disable this function. Isn't this exactly what enable_if is for?

I've put a full example here (and cpp.sh link that often fails)

I've found similar Q/As on SO, but generally those were more complicated and attempting something different.

like image 425
Matt Avatar asked Aug 29 '18 12:08

Matt


1 Answers

With C++20 and concept, you may use requires:

void notify_exit() requires has_member_function_notify_exit<Queue, void>::value;
like image 66
Jarod42 Avatar answered Sep 26 '22 18:09

Jarod42