Last active
December 17, 2018 18:07
-
-
Save badair/cd40fbc4db8feb7bb3ab9e70a53b7e74 to your computer and use it in GitHub Desktop.
function_type - reference implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
(c) copyright Barrett Adair 2017 | |
Distributed under the Boost Software License - Version 1.0 - August 17th, 2003 | |
Permission is hereby granted, free of charge, to any person or organization | |
obtaining a copy of the software and accompanying documentation covered by | |
this license (the "Software") to use, reproduce, display, distribute, | |
execute, and transmit the Software, and to prepare derivative works of the | |
Software, and to permit third-parties to whom the Software is furnished to | |
do so, all subject to the following: | |
The copyright notices in the Software and this entire statement, including | |
the above license grant, this restriction and the following disclaimer, | |
must be included in all copies of the Software, in whole or in part, and | |
all derivative works of the Software, unless such copies or derivative | |
works are solely in the form of machine-executable object code generated by | |
a source language processor. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
#include <experimental/type_traits> | |
namespace detail { | |
template<typename T> struct normalize {}; | |
template<typename R, typename... Args> struct normalize <R(Args...)> { template<typename T> using apply = R(T &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) &> { template<typename T> using apply = R(T &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) &&> { template<typename T> using apply = R(T &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...)> { template<typename T> using apply = R(T &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) &> { template<typename T> using apply = R(T &, Args...,...);}; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) &&> { template<typename T> using apply = R(T &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const> { template<typename T> using apply = R(T const &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const &> { template<typename T> using apply = R(T const &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const &&> { template<typename T> using apply = R(T const &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const> { template<typename T> using apply = R(T const &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const &> { template<typename T> using apply = R(T const &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const &&> { template<typename T> using apply = R(T const &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) volatile> { template<typename T> using apply = R(T volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) volatile &> { template<typename T> using apply = R(T volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) volatile &&> { template<typename T> using apply = R(T volatile &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) volatile> { template<typename T> using apply = R(T volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) volatile &> { template<typename T> using apply = R(T volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) volatile &&> { template<typename T> using apply = R(T volatile &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const volatile> { template<typename T> using apply = R(T const volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const volatile &> { template<typename T> using apply = R(T const volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const volatile &&> { template<typename T> using apply = R(T const volatile &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const volatile> { template<typename T> using apply = R(T const volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const volatile &> { template<typename T> using apply = R(T const volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const volatile &&> { template<typename T> using apply = R(T const volatile &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) noexcept> { template<typename T> using apply = R(T &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) & noexcept> { template<typename T> using apply = R(T &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) && noexcept> { template<typename T> using apply = R(T &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) noexcept> { template<typename T> using apply = R(T &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) & noexcept> { template<typename T> using apply = R(T &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) && noexcept> { template<typename T> using apply = R(T &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const noexcept> { template<typename T> using apply = R(T const &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const & noexcept> { template<typename T> using apply = R(T const &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const && noexcept> { template<typename T> using apply = R(T const &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const noexcept> { template<typename T> using apply = R(T const &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const & noexcept> { template<typename T> using apply = R(T const &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const && noexcept> { template<typename T> using apply = R(T const &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) volatile noexcept> { template<typename T> using apply = R(T volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) volatile & noexcept> { template<typename T> using apply = R(T volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) volatile && noexcept> { template<typename T> using apply = R(T volatile &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) volatile noexcept> { template<typename T> using apply = R(T volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) volatile & noexcept> { template<typename T> using apply = R(T volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) volatile && noexcept> { template<typename T> using apply = R(T volatile &&, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const volatile noexcept> { template<typename T> using apply = R(T const volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const volatile & noexcept> { template<typename T> using apply = R(T const volatile &, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...) const volatile && noexcept> { template<typename T> using apply = R(T const volatile &&, Args...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const volatile noexcept> { template<typename T> using apply = R(T const volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const volatile & noexcept> { template<typename T> using apply = R(T const volatile &, Args...,...); }; | |
template<typename R, typename... Args> struct normalize <R(Args...,...) const volatile && noexcept>{ template<typename T> using apply = R(T const volatile &&, Args...,...); }; | |
template<typename T> struct impl_base {}; | |
template<typename R, typename... Args> struct impl_base <R(Args...)> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) &> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) &&> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...)> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) &> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) &&> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const &> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const &&> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const &> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const &&> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) volatile> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) volatile &> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) volatile &&> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) volatile> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) volatile &> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) volatile &&> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const volatile> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const volatile &> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const volatile &&> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const volatile> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const volatile &> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const volatile &&> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) & noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) && noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) & noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) && noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const & noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const && noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const & noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const && noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) volatile noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) volatile & noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) volatile && noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) volatile noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) volatile & noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) volatile && noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const volatile noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const volatile & noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...) const volatile && noexcept> { using type = R(Args...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const volatile noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const volatile & noexcept> { using type = R(Args...,...); }; | |
template<typename R, typename... Args> struct impl_base <R(Args...,...) const volatile && noexcept>{ using type = R(Args...,...); }; | |
template<typename T> | |
using call_op = decltype(&T::operator()); | |
template<typename T, bool IsSimpleCallableClass = std::experimental::is_detected_v<call_op, T>> | |
struct function_type_impl : impl_base<T> {}; | |
template<typename T> | |
struct split_member {}; | |
template<typename T, typename C> | |
struct split_member<T C::*> { | |
using member_type = T; | |
}; | |
template<typename T> | |
using call_op_fn = typename split_member<call_op<T>>::member_type; | |
template<typename T> | |
struct function_type_impl<T, true> : impl_base<call_op_fn<T>> {}; | |
template<typename T, typename C> | |
struct function_type_impl<T C::*, false> { | |
using type = typename normalize<T>::template apply<C>; | |
}; | |
template<typename T> | |
struct function_type_impl<T*, false> : impl_base<T> {}; | |
template<typename T> | |
struct function_type_impl<T&, false> : function_type_impl<std::remove_cv_t<T>> {}; | |
template<typename T> | |
struct function_type_impl<T&&, false> : function_type_impl<std::remove_cv_t<T>> {}; | |
} // namespace detail | |
template<typename T> | |
struct function_type : detail::function_type_impl<std::remove_cv_t<T>> {}; | |
template<typename T> | |
using function_type_t = typename function_type<T>::type; | |
/*** tests ***/ | |
template<typename Input, typename ExpectedOuptut> | |
void check_function_type() { | |
static_assert(std::is_same_v<function_type_t<Input>, ExpectedOuptut>); | |
} | |
template<typename T> | |
struct is_substitution_failure { | |
template<typename> | |
static auto test(...) -> std::true_type; | |
template<typename A, function_type_t<A>* = nullptr> | |
static auto test(int) -> std::false_type; | |
static constexpr bool value = decltype(test<T>(0))::value; | |
}; | |
int main() { | |
check_function_type< | |
void (int, char, ...) const &, | |
void (int, char, ...) | |
>(); | |
check_function_type< | |
void (*)(int, char), | |
void (int, char) | |
>(); | |
check_function_type< | |
void (* const &)(int, char), | |
void (int, char) | |
>(); | |
check_function_type< | |
int (* &&)(), | |
int () | |
>(); | |
check_function_type< | |
int (* &&)(), | |
int () | |
>(); | |
check_function_type< | |
char const * const & (&)(...), | |
char const * const & (...) | |
>(); | |
struct foo; | |
check_function_type< | |
void (foo::* const &)(int, int, int) &, | |
void (foo &, int, int, int) | |
>(); | |
check_function_type< | |
void (foo::* const &)(int, int, int) &&, | |
void (foo &&, int, int, int) | |
>(); | |
check_function_type< | |
void (foo::*)(int, int, int) const, | |
void (foo const &, int, int, int) | |
>(); | |
check_function_type< | |
void (foo::*)(int, int, int), | |
void (foo &, int, int, int) | |
>(); | |
check_function_type< | |
void (foo::*)() noexcept, | |
void (foo &) | |
>(); | |
check_function_type< | |
void (foo::*)(int, int, int) const noexcept, | |
void (foo const &, int, int, int) | |
>(); | |
check_function_type< | |
void (foo::*)(int, int, int, ...) const noexcept, | |
void (foo const &, int, int, int, ...) | |
>(); | |
auto g = [](int, char){}; | |
using G = decltype(g); | |
using F = void(int, char); | |
check_function_type<G, F>(); | |
check_function_type<G const &, F>(); | |
check_function_type<G const &&, F>(); | |
check_function_type<G volatile &, F>(); | |
check_function_type<G &, F>(); | |
check_function_type<G &&, F>(); | |
check_function_type<decltype(&G::operator()), void(G const &, int, char)>(); | |
check_function_type<decltype(&G::operator()) const, void(G const &, int, char)>(); | |
check_function_type<decltype(&G::operator()) volatile &&, void(G const &, int, char)>(); | |
struct A { int operator() () volatile; }; | |
check_function_type<A, int ()>(); | |
struct B { void* operator() (...) volatile noexcept; }; | |
check_function_type<B, void* (...)>(); | |
static_assert(is_substitution_failure<int>::value); | |
static_assert(is_substitution_failure<int &>::value); | |
static_assert(is_substitution_failure<int (foo::** &)()>::value); | |
static_assert(is_substitution_failure<int (foo::** const)()>::value); | |
static_assert(is_substitution_failure<int (foo::** const &)()>::value); | |
static_assert(is_substitution_failure<int (foo::** volatile)()>::value); | |
static_assert(!is_substitution_failure<int (foo::* &)()>::value); | |
static_assert(!is_substitution_failure<int (foo::* const)()>::value); | |
static_assert(!is_substitution_failure<int (foo::* const &)()>::value); | |
static_assert(!is_substitution_failure<int (foo::* volatile)()>::value); | |
auto lambda = [](){}; | |
static_assert(!is_substitution_failure<decltype(lambda)&>::value); | |
static_assert(is_substitution_failure<void>::value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment