Skip to content

Instantly share code, notes, and snippets.

@xlc
Created April 24, 2014 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlc/11242000 to your computer and use it in GitHub Desktop.
Save xlc/11242000 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <typeinfo>
#include <type_traits>
#include <functional>
#include <tuple>
using namespace std;
int TestCount = 0;
struct Test
{
int t;
Test() { t = ++TestCount; cout << t << "Test()" << endl; }
~Test() { cout << "~Test()" << endl; }
Test(const Test &) { t = ++TestCount; cout << t << "Test(const Test &)" << endl; }
Test(Test &&) { cout << t << "Test(Test &&)" << endl; }
Test & operator=(const Test &) { cout << t << "operator=(const Test &)" << endl; }
Test & operator=(Test &&) { cout << t << "operator=(Test &&)" << endl; }
};
template <typename T>
struct callable_type : callable_type<decltype(&T::operator())>
{};
template <typename ClassType, typename ReturnType, typename... Args>
struct callable_type<ReturnType(ClassType::*)(Args...) const>
{
using return_type = ReturnType;
using argument_type = std::tuple<Args...>;
template<std::size_t I>
using arguments = std::tuple_element<I, argument_type>;
};
template <class T>
void test(T t)
{
t();
}
int main()
{
auto f = [](int i, float, bool){ return (double)i; };
cout << boolalpha
<< is_same<callable_type<decltype(f)>::arguments<0>::type, int>::value << endl
<< is_same<callable_type<decltype(f)>::arguments<1>::type, float>::value << endl
<< is_same<callable_type<decltype(f)>::arguments<2>::type, bool>::value << endl
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment