How to call overloaded member function from thread

YES, it is possible to call overloaded function and overloaded member function if we successfully resolve ambiguity of overloaded functions.

Let’s first check what’s an error it generate and why does it generate that error.

#include<iostream>
#include <thread>
using namespace std;
class Suggest
{
    public:
    void memberFunction(int arg1,int arg2)
    {
        cout<<"memberFunction :: "<<arg1 <<" "<<arg2<<endl;
    }
    void memberFunction(int arg1)
    {
        cout<<"memberFunction :: "<<arg1<<endl;
    }
};
int main()
{
    Suggest sObj;
    std::thread objThrd(&Suggest::memberFunction,sObj,10);
    objThrd.join();
    return 0;
}

Compile: g++ overloadFailure.cpp -o overloadFailure –lpthread

overloadFailure.cpp: In function âint main()â:

overloadFailure.cpp:20:61: error: no matching function for call to âstd::thread::thread(<unresolved overloaded function type>, Suggest&,  nt)â

         std::thread objThrd(&Suggest::memberFunction,sObj,10);                                                                 ^

Compile is not able to resolve name mangling conflict.

Solution: 

They are multiple ways to resolve above issue

  • Explicitly type-cast to correct member function using any cast method available.
  • use a workaround to call our member function with the help of lambda function.
#include<iostream>
#include <thread>
#include<string>
using namespace std;
class Suggest
{
    public:
    void memberFunction(string arg1,int arg2)
    {
        cout<<"2-Args memberFunction :: "<<arg1 <<", "<<arg2<<endl;
    }
    void memberFunction(string arg1)
    {
        cout<<"1-Arg memberFunction :: "<<arg1<<endl;
    }
};
int main()
{
    Suggest sObj;
    //Solution -1 :: by using static_cast<>
    //TYPECAST to the function that we want to call
    //we are suppose to call one argument function

    std::thread objThrd1 (static_cast<void (Suggest::*)(string, int)>(&Suggest::memberFunction),sObj,"using static_cast<>" ,4);
    objThrd1.join();

    std::thread objThrd2 (static_cast<void (Suggest::*)(string)>(&Suggest::memberFunction),sObj,"using static_cast<>");
    objThrd2.join();

    //Solution-2:: c-style typecast with typdef
    //we are suppose to call no argument function

    typedef void (Suggest::*MPF2ARGS)(string,int);
    std::thread objThrd3 ((MPF2ARGS) &Suggest::memberFunction),sObj,"using typedef",4);
    objThrd3.join();
    
    typedef void (Suggest::*MPF1ARG)(string);
    std::thread objThrd4 ((MPF1ARG)(&Suggest::memberFunction),sObj,"using typdef");
    objThrd4.join();

    //Solution-3:: c-style caste with function pointer variable
    //we are suppose to call no argument function
    //ptr is a pointer variable of type noArgumentFunc.

    MPF2ARGS ptr;
    ptr=&Suggest::memberFunction;
    std::thread objThrd5 (ptr,sObj,"using typedef function pointer variable",4);
    objThrd5.join();

    //Solution-4:: Using lambda function
    std::thread objThrd6([](Suggest s,string value, int i){ s.memberFunction (value,i);},sObj,"lamba function",4);
    objThrd6.join();

    std::thread objThrd7([](string value, Suggest s) { s.memberFunction(value);},"lamba function",sObj);
    objThrd7.join();

    //Solution-5:: Using auto keyword
    auto fName=[](Suggest s,string value, int i){s.memberFunction(value,i);};
    std::thread objThrd8(fName,sObj,"lamba function",4);
    objThrd8.join();

    //Solution-6:: using using keyword
    using uNameFunc=void (Suggest::*)(string, int);
    uNameFunc uVariable=&Suggest::memberFunction;
    std::thread objThrd9(uVariable,sObj,"using using keyword", 4);
    objThrd9.join();

    return 0;
}

Compile: g++ overloadMember.cpp -o overloadMember -lpthread

Output:

./overloadMember

2-Args memberFunction :: using static_cast<>, 4

1-Arg memberFunction :: using static_cast<>

2-Args memberFunction :: using typedef, 4

1-Arg memberFunction :: using typdef

2-Args memberFunction :: using typedef function pointer variable, 4

2-Args memberFunction :: lamba function, 4

1-Arg memberFunction :: lamba function

2-Args memberFunction :: lamba function, 4

2-Args memberFunction :: using using keyword, 4

                                                             ^



PREV::How to call overridden VIRTUAL function from thread

Your Comments /Suggestions & Questions are always welcome. 

We would like to help you with best of our knowledge.

So feel free to put Questions.

No comments:

Post a Comment