Check for template function

How to check for the existence of a template function as follows: Check if a readerstruct has an readarithmetic value

struct reader {
    template<typename T>
    std::enable_if_t<std::is_arithmetic<T>::value, T> read() {
        return {};
    }
};

I use checker like this:

template <typename T>
struct test_read {
    static constexpr auto value = std::is_convertible<decltype(std::declval<T>().read<int>()),int>::value;      
};

But the compiler complains:

error: wrong number of template arguments (1, should be 2)
 static constexpr auto value = std::is_convertible<decltype(std::declval<T>().read<int>()),int>::value;

Please give me your advice.

Thank.


Update: here is the final version that I got after the discussion, I hope everyone finds it useful for your code

struct not_reader {

};


struct reader {
    template<typename T>
    std::enable_if_t<std::is_arithmetic<T>::value, T> read() {
        return {};
    }
};


template<class T, class Elem>
struct has_read {
private:
    template<class C, typename=void>
    struct test_read : std::false_type {
    };
    template<class C>
    struct test_read<C, typename std::enable_if<std::is_convertible<decltype(std::declval<C>().template read<Elem>()), Elem>::value>::type>
            : std::true_type {
    };
public:
    using type = typename test_read<T>::type;
    static constexpr bool value = test_read<T>::value;
};


static_assert(has_read<reader, int>::value, "reader should have int read()");
static_assert(!has_read<not_reader, int>::value, "not_reader should not have int read()");
+4
source share
2 answers

You forgot templatebeforeread()

static constexpr auto value
    = std::is_convertible<
         decltype(std::declval<T>().template read<int>()),int>::value;  
// .................................#########    

, "if reader struct read ": test_read int, .

#include <type_traits>

struct reader
 {
   template<typename T>
   std::enable_if_t<std::is_arithmetic<T>::value, T> read()
    { return {}; }
 };

template <typename, typename = void>
struct readTypeRet
 { using type = void; };

template <typename T>
struct readTypeRet<T, decltype(std::declval<T>().template read<int>(), void())>
 { using type = decltype(std::declval<T>().template read<int>()); };

template <typename T>
struct test_read
    : public std::is_convertible<typename readTypeRet<T>::type, int>
 { };

int main ()
 {
   static_assert(test_read<reader>::value == true, "!");
   static_assert(test_read<int>::value == false,   "!");
 }
+4

:

  • , T, T is_arithmetic, void
  • , int int

, - std::result_of (++ 11/14, std::invoke_result_t ++ 17):

template<class T>
struct test_read {
static constexpr auto value = std::is_convertible<
    typename std::result_of<decltype(&T::template read<int>)(T)>::type, int
    >::value;    
};

Live Demo

:

  • - read T (reader) template , reader .
  • result_of F(Args), reader::read F, reader Args
    • T (reader) read, - ( static ), - , .
+1

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


All Articles