std::function initialization for overloaded member function in a class [duplicate] - c++

This question already has answers here:
How do I specify a pointer to an overloaded function?
(6 answers)
Closed 3 years ago.
How do we create a std::function object for overloaded member functions in a class? std::function object can be created for a non-overloaded member function as shown.
Sample code is attached as shown below
#include<iostream>
#include<functional>
class X
{
public:
X(){}
virtual ~X(){}
void foo1(char c)
{std::cout<<"X::foo1(char c)\n";}
void foo2(char c)
{std::cout<<"X::foo2(char c)\n";}
void foo2(int i)
{std::cout<<"X::foo2(int i)\n";}
}x;
int main()
{
std::function<void(X*,char)> f1=&X::foo1;
f1(&x,'a');
/* //The following code doesn't work
std::function<void(X*,char)> f2=&X::foo2;
f2(&x,'a');
*/
return 0;
}
Following error is given :
conversion from '' to non-scalar type 'std::function' requested

You need to be explicit about the signature of the overloaded function you intend to use:
std::function<void(X*,char)> f2 = static_cast<void (X::*)(char)>(&X::foo2);
As an alternative, use a lambda:
std::function<void(X*,char)> f3 =
[](X *instance, char c){ return instance->foo2(c); };

Related

In c++ how do i refer a member function of a class to a non member function which is further being called by another member function of that class? [duplicate]

This question already has answers here:
Function pointer to member function
(8 answers)
How Can I Pass a Member Function to a Function Pointer?
(3 answers)
Closed 2 years ago.
In c++ how do i refer a member function of a class to a non member function which is further being called by another member function of that class?
This is my code and it return into this error:
[Error] cannot convert 'void (example::*)()' to 'void (*)(int)' for argument '2' to 'int non_member_func(int, void (*)(int))'
#include<iostream>
int non_member_func(int,void func(int))
{
return 0;
}
class example
{
public:
void member_func1()
{
}
void member_func2(int x)
{
int num = non_member_func(1,&this->member_func1);
}
}object;
int main()
{
object.member_func1();
return 0;
}

C++: std::apply with overloaded member function [duplicate]

This question already has answers here:
Is there any way in C++ to refer to a function template while neither calling it nor supplying its template parameters?
(4 answers)
Convert template function to generic lambda
(2 answers)
How do I make a functor out of an arbitrary function?
(2 answers)
Closed 2 years ago.
I have a function returning a std::pair, which I want to use to call an overloaded member function. I am not aware of any way to unpack a tuple in-place (which I'd prefer), so std::apply is my best bet, I think.
This works with a combination of tuple_cat and forward_as_tuple when the member function is not overloaded, but otherwise, the compiler complains about an unresolved overload.
That is understandable, given that std::apply just takes a function pointer, but I'd still like to know whether it can be made to work in a readable way.
Here's some example code:
#include <iostream>
#include <tuple>
#include <utility>
std::pair<int, int> func() { return { 0, 1 }; }
class MyClass {
public:
int memFun(int);
int memFun(int, int);
};
int MyClass::memFun(int i){ return 2*i; }
int MyClass::memFun(int a, int b){ return a+b; }
int main(){
MyClass myClass;
/* works */
std::pair pair { func() };
int number { myClass.memFun( pair.first, pair.second ) };
/* doesn't work (unresolved overload) */
int number { std::apply(
&MyClass::memFun,
std::tuple_cat( std::forward_as_tuple(myClass), func() )
) };
std::cout << number << "\n";
return 0;
}
Is there an elegant (=readable) way to do this without a temporary?
I only found this question and so far don't think it fully applies.

How create a function pointer to point a member function? [duplicate]

This question already has answers here:
function pointer to a class member
(2 answers)
Closed 9 years ago.
Code:
struct A
{
void* f(void *)
{
}
};
int main()
{
void * (*fp)(void *);
fp = &A::f;
}
Compile:
|12|error: cannot convert 'void* (A::*)(void*)' to 'void* (*)(void*)' in assignment|
I have tried many ways, but all failed... How to do that?
For a regular member pointer, you'll need to declare the type it's a member of on the pointer type;
int main()
{
void * (A::*fp)(void *);
fp = &A::f;
}
Don't use function pointers use std::mem_fn
auto func = std::mem_fn(&A::f);
If you really want to use function pointers(you don't) you have to do this.
void * (A::*func)(void*);
func = &A::f;
IMO this is uglier, harder to read and less maintainable
You can also use std::function like this.
A a;
std::function<void*(const A &, void*)> f_add_display = &Foo::print_add;
but mem_fn is better suited for the job since you have a memfunction
std::function<void *( A&, void *)> fp = &A::f;

C++ overloaded method pointer [duplicate]

This question already has answers here:
How do I specify a pointer to an overloaded function?
(6 answers)
Closed 7 years ago.
How do I get a method pointer to a particular overload of a method:
struct A {
void f();
void f(int);
void g();
};
I know that
&A::g
is a pointer to g. But how do I get a pointer to f or f(int)?
(void (A::*)()) &A::f
(void (A::*)(int)) &A::f
function pointers and member function pointers have this feature - the overload can be resolved by to what the result was assigned or cast.
If the functions are static, then you should treat them as ordinary functions:
(void (*)()) &A::f;
(void (*)(int)) &A::f;
or even
(void (*)()) A::f;
(void (*)(int)) A::f;
You just have to cast the result of &A::f in order to remove the ambiguity :
static_cast<void (A::*)()>(&A::f); // pointer to parameterless f
static_cast<void (A::*)(int)>(&A::f); // pointer to f which takes an int
Thanks to Stefan Pabst for the following idea, which he presented in a five minute lightning talk at ACCU 2015. I extended it with tag types to allow for resolving overloads by their cv qualifier and/or reference qualifier, and a C++17 variable template to avoid having to type the extra pair of parentheses which is otherwise required.
This solution works on the same principle as the cast-based answers, but you avoid having to restate either the return type of the function or, in the case of member functions, the name of the class which the function is a member of, as the compiler is able to deduce these things.
bool free_func(int, int) { return 42; }
char free_func(int, float) { return true; }
struct foo {
void mem_func(int) {}
void mem_func(int) const {}
void mem_func(long double) const {}
};
int main() {
auto f1 = underload<int, float>(free_func);
auto f2 = underload<long double>(&foo::mem_func);
auto f3 = underload<cv_none, int>(&foo::mem_func);
auto f4 = underload<cv_const, int>(&foo::mem_func);
}
The code implementing the underload template is here.

how to make a operator have an explicit parameter c++ [duplicate]

This question already has answers here:
Overload resolution with std::function
(2 answers)
Closed 6 years ago.
consider some code:
void foo() { }
bool bar() { return true; }
struct S
{
void operator=(std::function<void()> f){f();};
void operator=(std::function<bool()> f){f();};
};
int main() {
S s;
s = foo; // ok
s = bar; // error: use of overloaded operator '=' is ambiguous
}
How can I make this example unambiguous?
The problem you're running into is that std::function<void(Args...)> is allowed to discard return types - so both std::function<void()> and std::function<bool()> can be constructed from a bool(*)(). The latter will forward through the return from bar, but the former will just discard it. That's perfectly valid behavior, but causes this to be ambiguous.
What you probably want is to avoid std::function altogether:
template <class F>
void operator=(F f) { f(); }