Home > Net >  Changing the lambda function but the lambda argument and return type stays the same
Changing the lambda function but the lambda argument and return type stays the same

Time:06-14

I am using std::semiregular to hold some functors in a class. Ideally, what I really want is to be able to instantiate such template class, but define the lambda implementation at a later stage using the register function. However, I am struggling to find a way do that.

Even the simplest case down below does not seem to work.

The compiler error says:

error: cannot convert 'main()::<lambda(int, float)>' to 'main()::<lambda(int, float)>'    30 |     api.register_get(get1);
      |                      ^~~~
      |                      |
      |                      main()::<lambda(int, float)>
#include <concepts>
#include <functional>
#include <iostream>



template<std::semiregular F>
class RestApiImpl {
    F m_get_method;
    public:
    RestApiImpl(F get = F{}) : m_get_method{std::move(get)} {}
    void register_get(F functor) {
       m_get_method = std::move(functor);
    }
};



int main(){
    auto get = [](int,   float intf){ 
            std::string dummy = "dummy";
    };
    RestApiImpl api(get);
    
    auto get1 = [](int,  float intf){ 
            std::string dummy = "dummy";
    };
    
    
    api.register_get(get1); 
    
    
    return 0;
};

CodePudding user response:

That's because the type of the lambdas are different. You could use a function pointer, or a std::function.
I believe the following change is valid, and should be the only required one:

RestApiImpl<void(*)(int,float)> api(get);

The only difference from your code is the template parameter type is explicitly specified.
The type is pointer to a void returning function accepting int for the first parameter, and float for the second. A pointer to main would be int(*main)(int,char**)
If you use std::function then the type would be std::function<void(int,float)>.

  • Related