lundi 28 décembre 2015

Lambda type saying it doesn't have an operator()?

Getting the following error in the following code and I don't see why. Shouldn't the lambda have an operator() method? Basically I am trying to capture the return type of the lambda operator() and the args type and crate a new typedef that the MakeFoo will be returning by forwarding the lambda on to Foo's constructor and then returning the object. See below.

#include <functional>                                                                                 
#include <tuple>                                                                                      
#include <iostream>                                                                                   

template <typename T>                                                                                 
class Foo;                                                                                            

template <typename R, typename... Args>                                                               
struct Foo<R(Args...)> {                                                                              

   template <typename T>                                                                              
   Foo(T aT) {                                                                                        
   }                                                                                                  
};                                                                                                    

template <typename LambdaT>                                                                           
struct function_traits                                                                                
   : public function_traits<decltype(&LambdaT::operator())> {};                                       

template <typename ClassType, typename ReturnType, typename... Args>                                  
struct function_traits<ReturnType (ClassType::*)(Args...) const> {                                    
   using FooType = Foo<ReturnType(Args...)>;                                                          
};                                                                                                    

template <typename ClassType, typename ReturnType, typename... Args>                                  
struct function_traits<ReturnType (ClassType::*)(Args...)> {                                          
   using FooType = Foo<ReturnType(Args...)>;                                                          
};                                                                                                    

template <typename ReturnType, typename... Args>                                                      
struct function_traits<ReturnType (*)(Args...)> {                                                     
   using FooType = Foo<ReturnType(Args...)>;                                                          
};                                                                                                    

template <typename T>                                                                                 
auto MakeFoo(T&& lambda) ->                                                                           
   typename function_traits<decltype(lambda)>::FooType {                                              
   return function_traits<decltype(lambda)>::FooType(std::forward<T>(lambda));                        
}                                                                                                     

// test code below:                                                                                   
int main() {                                                                                          
   int z = 001;                                                                                       
   auto lambda = [z](int x) { std::cout << " test " << std::endl; };                                  

   using traits = function_traits<decltype(lambda)>;                                                  

   auto f = MakeFoo(lambda);                                                                          

   return 0;                                                                                          
}    





g++ -g3  -std=c++14 -pedantic -Werror -Wno-invalid-offsetof -fno-omit-frame-pointer -march=ivybridge -mtune=ivybridge -mno-avx2 -mavx -fl
o -ffat-lto-objects -fuse-linker-plugin -fno-strict-aliasing -c main.cpp                                                                 
main.cpp: In instantiation of ‘struct function_traits<main()::<lambda(int)>&>’:                                                          
main.cpp:36:6:   required by substitution of ‘template<class T> typename function_traits<decltype (lambda)>::FooType MakeFoo(T&&) [with T
= main()::<lambda(int)>&]’                                                                                                               
main.cpp:48:27:   required from here                                                                                                     
main.cpp:18:38: error: ‘operator()’ is not a member of ‘main()::<lambda(int)>&’                                                          
    : public function_traits<decltype(&LambdaT::operator())> {};                                                                         
                                      ^                                         

Aucun commentaire:

Enregistrer un commentaire