So far I've been able to detect whether a function exists in a class by name, without needing its signature, as follows:
Following piece of code heavily "inspired" by the answer from this question: Is it possible to figure out the parameter type and return type of a lambda?
I understand that different specializations should be created for different modifiers, such as const member functions.
template <typename T, typename TReturn, typename... Args>
struct function_traits<TReturn( T::* )( Args... )>
{
using return_type = TReturn;
using arguments = std::tuple<Args...>;
static std::size_t constexpr arg_count = sizeof...( Args );
template <std::size_t N, typename = void>
struct argument
{
using type = std::tuple_element_t<N, arguments>;
};
template <std::size_t N>
struct argument<N, std::enable_if_t<( !( N < arg_count ) ), void>> {};
};
And my own code as a workaround for 28.4.4 of The C++ Programming Language, 4th Edition since I am using VC++2015.
template <typename T>
struct has_func_f
{
private:
template <typename C, typename = void>
struct get_result
{
static bool constexpr value = false;
};
// need <function_name> as a class parameter so I can append and get &C::<function_name>
template <typename C>
struct get_result<C, std::enable_if_t<( std::is_same<decltype( ( decltype( std::declval<typename function_traits<decltype( &C::f )>::arguments>() ) ) ( std::declval<typename function_traits<decltype( &C::f )>::arguments>() ), std::true_type() ), std::true_type>::value ), void>>
{
static bool constexpr value = true;
};
public:
static bool constexpr value = get_result<T>::value;
};
This code works for detecting any function signature as long as the name matches. My example usage:
struct A {};
struct B { float f(int, double); };
struct C { void f(); };
int main()
{
has_func_f<A>::value; // false
has_func_f<B>::value; // true
has_func_f<C>::value; // true
return 0;
}
So as my question states, how can I possibly do what I want to do? I've looked and looked and the closest thing I've come to is using a macro to generate the appropriate struct for the function name I want. Is there any other way?