O C ++ Lambda não possui operator ()
Eu preciso de um método para descobrir os tipos de argumento de uma função e, portanto, escrevi uma classe encerramento_traits, fornecida abaixo, inspirada emÉ possível descobrir o tipo de parâmetro e o tipo de retorno de um lambda?.
No entanto, quando tento aplicá-lo a um lambda simples, recebo o erro de que 'operator ()' não é membro de '(tipo lambda)'. No entanto, de acordo com a cppreference, os lambda têm um operador (). Eu também tentei usar std :: function e recebi o erro equivalente. Acho que não tenho certeza do que está errado, e qualquer ajuda seria muito apreciada.
#include<type_traits>
#include<tuple>
#include<utility>
#include<iostream>
/* For generic types use the type signature of their operator() */
template <typename T>
struct closure_traits : public
closure_traits<decltype(&T::operator())> {};
/* Otherwise, we do a template match on a function type. */
template <typename ClassType, typename ReturnType,
typename... ArgTypes>
struct closure_traits<ReturnType (ClassType::*) (ArgTypes... args)>
{
using arity = std::integral_constant<std::size_t,
sizeof...(ArgTypes)>;
using Ret = ReturnType;
/* The argument types will be the same as the types of the
* elements of a tuple composed of them.
*/
template <std::size_t I>
struct Args {
using type = typename std::tuple_element<I,
std::tuple<ArgTypes...>>::type;
};
};
int main() {
auto thing = [=] (int x) {return x;};
std::cerr << "The number of arguments is "
<< closure_traits<decltype(thing)>::arity << std::endl;
return 0;
}
As mensagens de erro do compilador que recebo estão abaixo. Meu comando de compilação é simplesmente g ++ -std = c ++ 14 main.cpp.
main.cpp: In instantiation of ‘struct closure_traits<int (main()::<lambda(int)>::*)(int) const>’:
main.cpp:9:8: required from ‘struct closure_traits<main()::<lambda(int)> >’
main.cpp:34:82: required from here
main.cpp:9:56: error: ‘operator()’ is not a member of ‘int (main()::<lambda(int)>::*)(int) const’
struct closure_traits : public closure_traits<decltype(&T::operator())> {};
^
main.cpp: In function ‘int main()’:
main.cpp:34:51: error: ‘arity’ is not a member of ‘closure_traits<main()::<lambda(int)> >’
std::cerr << "The number of arguments is " << closure_traits<decltype(thing)>::arity << std::endl;