Why does this C ++ characteristic detect that type T has a void operator (EDT const &)?

I am trying to use SFINAE to determine if a type passed as an argument to a T template had T :: operator () (P const &), where P is also a template argument. I modeled my solution after this Idoom Member Detector example . Unfortunately, I could not get this to work for the operator (), although I could get it to function for the normal method.

Here is a sample code that demonstrates the problem I am facing:

#include <iostream>
#include <iomanip>
#include <utility>
#include <type_traits>

using namespace std;

struct has
{
    void operator()(int const&);    
};

struct hasNot1
{
    void operator()(int);   
};

struct hasNot2
{
    void operator()();  
};

struct hasNot3
{
    void operator()(float); 
};

struct hasNot4
{
};

template<typename T, typename EDT>
struct is_callable_oper
{
      private:
                    typedef char(&yes)[1];
                    typedef char(&no)[2];

                    template <typename U, void (U::*)(EDT const &)> struct
                                                                        Check;
                    template<typename>
                    static yes test(...);

                    template <typename U>
                    static no
                            test(Check<U, &U::operator()>*);

                public:
                    static constexpr bool value = sizeof(test<T>(0))
                                                                == sizeof(yes);
};

int main() {
    cout << boolalpha << is_callable_oper<has, int&>::value << " " 
         << is_callable_oper<has, int>::value << " "
         << is_callable_oper<hasNot1, int&>::value << " "
         << is_callable_oper<hasNot2, int&>::value << " "
         << is_callable_oper<hasNot3, int&>::value << " "
         << is_callable_oper<hasNot4, int&>::value << endl;
    return 0;
}

Running on ideon ( https://ideone.com/tE49xR ): true false true true true true

I expected: true false false false false false

Completed work:

: gcc 4.6.3 ++ 0x.

: , .

: , , . , , , .

  • SFINAE declval, ?

  • ? , : void (operator())(EDT const&)?

  • CV- , , . \

  • Boost . , Boost 1.43 ( ), . , .

, , . , , , , . .


1


@Nir Friedman, , , . , . , .


2


, @Sam Varshavchik , . - , EDIT 1, , .

+4
2

, operator() void, , .

, false true false false false false, true false false false false false:

 is_callable_oper<has, int&>::value

has::operator() int & const &, int &, .

 is_callable_oper<has, int>

has operator(), const int &, .

std::is_same , std::enable_if - SFINAE - .

#include <type_traits>
#include <iostream>

struct has
{
    void operator()(int const&);
};

struct hasNot1
{
    void operator()(int);
};

struct hasNot2
{
    void operator()();
};

struct hasNot3
{
    void operator()(float);
};

struct hasNot4
{
};

template<typename T, typename P, typename foo=void>
class is_callable_oper : public std::false_type {};

template<typename T, typename P>
class is_callable_oper<T, P, typename std::enable_if<
         std::is_same<decltype(&T::operator()),
                      void (T::*)(const P &)>::value>::type>
    : public std::true_type {};

int main() {
    std::cout << std::boolalpha << is_callable_oper<has, int&>::value << " "
         << is_callable_oper<has, int>::value << " "
         << is_callable_oper<hasNot1, int&>::value << " "
         << is_callable_oper<hasNot2, int&>::value << " "
         << is_callable_oper<hasNot3, int&>::value << " "
          << is_callable_oper<hasNot4, int&>::value << std::endl;
    return 0;
}

EDIT: std::void_t , :

template<typename T, typename P>
class is_callable_oper<T, P,
        std::void_t<decltype( std::declval< void (T::*&)(const P &)>()=&T::operator())>>
    : public std::true_type {};
+2

void_t idiom: ` void_t`. . 1:

template <class T>
using void_t = void;  // avoid variadics for simplicity

, , 17.

false:

template <class T, class P, class = void>
struct is_callable_oper : std::false_type {};

, , .

template <class T, class P>
struct is_callable_oper<T, P, 
    void_t<decltype(std::declval<T>()(std::declval<P>()))>>: std::true_type {};

, void_t, , void. , ; . , , ,

std::declval<T>()(declval<P>())

. , . , , lvalues, rvalues, constness, , .

, , , . , true true false true false. : http://coliru.stacked-crooked.com/a/81ba8b208b90a2ba.

, , is_callable_oper<has, int>::value false. , , , const int& int. .

+2

Source: https://habr.com/ru/post/1671372/


All Articles