I was trying to wrap my head around this question here because it was written in such a way that it was hiding what it was actually doing. So I rewrote it as such:
template<typename CLASS>
struct has_begin
{
// NOTE: sig_matches() must come before fn_exists() as it is used for its
// type. Also, no function bodies are needed as they are never called.
// This matching sig results in a return type of true_type
template<typename A_CLASS>
static auto
sig_matches(void(A_CLASS::*)())
-> std::true_type;
// If the member function A_CLASS::begin exists and a sig_matches() function
// exists with the required sig, then the return type is the return type of
// sig_matches(), otherwise this function can't exist because at least one
// the types don't exist so match against fn_exists(...).
template <typename A_CLASS>
static auto
fn_exists(decltype(&A_CLASS::begin))
-> decltype(sig_matches<A_CLASS>(&A_CLASS::begin));
// Member function either doesn't exist or doesn't match against a
// sig_matches() function.
template<typename A_CLASS>
static auto
fn_exists(...)
-> std::false_type;
// Intermediate storage of type for clarity
typedef decltype(fn_exists<CLASS>(nullptr)) type;
// Storing the resulting value
static int const value = type::value;
};
After doing that, it was fairly easy what was happening. However, I found something odd. If a class was passed to this with 2 begin signatures, one of which matched against has_begin::sig_matches()
, it would fail to match against it.
#include <iostream>
#include <type_traits>
struct A
{
void begin()
{
std::cout << "begin() called 1" << std::endl;
}
};
struct B {};
struct C
{
void begin()
{
std::cout << "begin() called 1" << std::endl;
}
void begin(float)
{
std::cout << "begin() called 2" << std::endl;
}
};
template<typename T, typename...ARGs>
typename std::enable_if<!!has_begin<T>::value>::type
call(ARGs...args)
{
std::cout << "Found(" << has_begin<T>::value << ")" << std::endl;
T().begin(args...);
}
template<typename T, typename...ARGs>
typename std::enable_if<!has_begin<T>::value>::type
call(ARGs...)
{
std::cout << "NOT Found(" << has_begin<T>::value << ")" << std::endl;
}
int main()
{
call<A>(); // A::begin() called
call<B>(); // B has no begin()
call<C>(); // C::begin() is not called.
return 0;
}
Why is it failing to match against C::begin()
?
Edit
The reason is that &A_CLASS::begin
is ambiguous. The corrected class is as follows:
template<typename CLASS>
struct has_begin
{
// NOTE: No function bodies are needed as they are never called.
// If the member function A_CLASS::begin exists with the required sig,
// then the return type is true_type otherwise this function can't
// exist because the type cannot be deduced.
template <typename A_CLASS>
static auto
fn_exists(decltype((void(A_CLASS::*)())&A_CLASS::begin))
-> std::true_type;
// Member function either doesn't exist or doesn't match against the
// required signature
template<typename A_CLASS>
static auto
fn_exists(...)
-> std::false_type;
// Intermediate storage of type for clarity
typedef decltype(fn_exists<CLASS>(nullptr)) type;
// Storing the resulting value
static int const value = type::value;
};
Yakk and dyp brought up a good point. Here is a way to do the same but with a compatible signature:
template<typename CLASS>
struct has_begin
{
// NOTE: No function bodies are needed as they are never called.
// If the member function A_CLASS::begin exists that has a compatible sig,
// then the return type is true_type otherwise this function can't exist
// because the type cannot be deduced.
template <typename A_CLASS>
static auto
fn_exists(decltype(std::declval<A_CLASS>().begin())*)
-> std::true_type;
// Member function either doesn't exist or doesn't match against the
// required compatible signature
template<typename A_CLASS>
static auto
fn_exists(...)
-> std::false_type;
// Intermediate storage of type for clarity
typedef decltype(fn_exists<CLASS>(nullptr)) type;
// Storing the resulting value
static int const value = type::value;
};
I find this cleaner than Yakks answer as it doesn't require detail namespaces and other 'noise', but YYMV.
This is C++11. Stop doing that C++03 gymnastics.
the above is write-once boilerplate. It gives you a
can_apply<template, Ts...>
helper that makes writing "do we have a method" and other similar tests easy:and now
has_begin<X>{}
is true if and only if X can be called with.begin()
.This also fixes a flaw in your code.
will fail your test, but pass mine.
Replace
By
As
Live demo