question

flaviu avatar image
0 Votes"
flaviu asked flaviu edited

cannot convert from 'const _Ty2' to 'const std::function

I have a bottleneck to my code:

Code:

 void CSomeClass::SomeMethod(const std::string& method, const std::function<void (CMyObject)>& handler)
 {
     //
 }
    
 void CSomeClass::RegisterHandler(const web::http::method& Method, const MethodFn& Fn)
 {
     m_methodHandlerMap[Method] = Fn;
 }

 MethodHandlerMap CMyClass::GetHandlerMap() const
 {
     return m_methodHandlerMap;
 }

 using MethodFn = std::function<void(CMyObject&)>;
 using MethodHandlerMap = std::unordered_map<std::string, MethodFn>;
 MethodHandlerMap m_methodHandlerMap;

 // code block
 {
     CSomeClass sc;
     sc.RegisterHandler("AMethod", std::bind(&CSomeClassExtended::AMethod, this, std::placeholders::_1));
 }

 // code block
 {
     auto localMap = m_pMyClass->GetHandlerMap();

     std::for_each(localMap.begin(), localMap.end(),
                 [this](const auto& handlePair)
                 {
                     m_object.SomeMethod(handlePair.first, handlePair.second);    // ERROR !!! see below
                 });
 }

and the error I got:

 // error C2664: 'void CSomeClass::SomeMethod(const std::string &, const std::function<void (CMyObject)> &)': cannot convert argument 2 from 'const _Ty2' to 'const std::function<void (CMyObject)> &'
 // note: Reason: cannot convert from 'const _Ty2' to 'const std::function<void (CMyObject)>'

the fact is that I write:


     std::for_each(localMap.begin(), localMap.end(),
                 [this](const auto& handlePair)
                 {
                     m_object.SomeMethod(handlePair.first, nullptr);        // is compiling ok
                 });

I didn't write the lambda correctly ? What I have done wrong ?












c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IgorTandetnik-1300 avatar image
1 Vote"
IgorTandetnik-1300 answered

You are passing std::function<void (CMyObject&)> to a function that expects std::function<void (CMyObject)> (note the extra ampersand). These are distinct, unrelated types.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered flaviu commented

Check this:

 void CSomeClass::SomeMethod( const std::string& method, const std::function<void( CMyObject& )>& handler )...

 or:

 void CSomeClass::SomeMethod( const std::string& method, const MethodFn & handler )...



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you Viorel, consider that you also give the solution !

0 Votes 0 ·
flaviu avatar image
0 Votes"
flaviu answered flaviu edited

I cannot change CSomeClass::SomeMethod signature because is a part of a library. But I would take care of what you have told me.

You told me both of you the source of the error, how can accept both answers as solution ? I will take on of you, but consider that both helped me to solve it ! Thanks a lot !

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe you can adjust the definition of MethodFn?

1 Vote 1 ·

That what I did. Thank you a lot !

0 Votes 0 ·