Skip to content

Instantly share code, notes, and snippets.

@gatchamix
Last active August 18, 2017 09:40
Show Gist options
  • Save gatchamix/f62f4d718094dedd9d40515749bb2543 to your computer and use it in GitHub Desktop.
Save gatchamix/f62f4d718094dedd9d40515749bb2543 to your computer and use it in GitHub Desktop.
member function exact signature matching
#include <type_traits>
#include <functional>
//
struct validator
{
template <template <class...> class, class...>
static auto constexpr is_valid(...)
-> std::false_type;
template <template <class...> class Op, class... Args, class = Op<Args...>>
static auto constexpr is_valid(int)
-> std::true_type;
};
template <template <class...> class Op, class... Args>
using is_valid = decltype(validator::is_valid<Op, Args...>(int{}));
template <template <class...> class Op, class... Args>
auto constexpr is_valid_v = is_valid<Op, Args...>::value;
//
template <class T, class Sig>
using has_func = decltype(std::mem_fn<Sig>(&T::func));
template <class T>
struct base
{
constexpr base()
{
static_assert(is_valid_v<has_func, T, void()>);
static_assert(is_valid_v<has_func, T, int(double)>);
static_assert(is_valid_v<has_func, T, double(int)>);
static_assert(is_valid_v<has_func, T, void(int, int)>);
static_assert(!is_valid_v<has_func, T, void(int)>);
static_assert(is_valid_v<has_func, T, void() const>);
static_assert(is_valid_v<has_func, T, int(double) const>);
static_assert(is_valid_v<has_func, T, double(int) const>);
static_assert(is_valid_v<has_func, T, void(int, int) const>);
static_assert(!is_valid_v<has_func, T, void(int) const>);
}
};
template <template <class> class T>
struct derived : public T<derived<T>>
{
using base_t = T<derived<T>>;
friend struct validator;
public:
void func() {}
void func() const {}
void func(int, int) {}
void func(int, int) const {}
protected:
int func(double) {}
int func(double) const {}
private:
double func(int) {}
double func(int) const {}
};
//
int main()
{
volatile auto constexpr test = derived<base>{};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment