关于Is it possible to figure out the parameter type and return type of a lambda?的后续问题。我测试了代码并且它对我来说很好但只有当我从main()
调用它时,当我从Do
调用代码时代码无法编译时,如果你可以取消注释行,请告诉我在Do
函数中编译代码
#include <tuple>
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
#include <memory>
template <class T>
std::string GetTypeName()
{
// http://stackoverflow.com/questions/23266391/find-out-the-type-of-auto/23266701#23266701
std::unique_ptr<char, void(*)(void*)> name{abi::__cxa_demangle(typeid(T).name(), 0, 0, nullptr), std::free};
return name.get();
}
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
{
enum { arity = sizeof...(Args) };
typedef ReturnType result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
template <typename FuncType>
void Do(FuncType func)
{
typedef function_traits<decltype(func)> traits;
//typename traits::arg<1>::type x = 0;
std::cout << GetTypeName<traits>() << std::endl;
//std::cout << GetTypeName<traits::arg<1>::type>() << std::endl;
}
int main()
{
auto func = [] (int x, float y, double z) -> void { };
Do(func);
typedef function_traits<decltype(func)> traits;
traits::arg<1>::type x = 0;
std::cout << GetTypeName<traits>() << std::endl;
std::cout << GetTypeName<traits::arg<1>::type>() << std::endl;
return 0;
}
编译错误是
test.cpp: In function ‘void Do(FuncType)’:
test.cpp:37:20: error: non-template ‘arg’ used as template
typename traits::arg<1>::type x = 0;
^
test.cpp:37:20: note: use ‘traits::template arg’ to indicate that it is a template
test.cpp:37:20: error: declaration does not declare anything [-fpermissive]
我使用g++ (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6)
,编译命令为g++ -std=c++11 test.cpp -o test