This question already has answers here:
How to make a function return a pointer to a function? (C++)
(14 answers)
Closed 3 years ago.
How to return an void (*)(void) variable from a function?
More precisely how to mark the return type?
???? getFunc(){
void (*pt2Func)(void) = ...;
return pt2Func;
}
void main(){
void (*myFunc)(void) = getFunc();
myFunc();
}
The most readable approach is via a type alias:
using VoidFct = void (*)(void);
VoidFct getFunc(){
return &f;
}
where the function to which a pointer is passed could be
#include <cstdio>
void f() {
std::puts("laksjdf");
}
and the client could would be
VoidFct g = getFunc();
g();
Related
This question already has answers here:
C++ return value without return statement
(6 answers)
C++ - Program returning values without return statement [duplicate]
(2 answers)
Return value from writing an unused parameter when falling off the end of a non-void function
(1 answer)
Closed 9 months ago.
Why p() fucntion return a value? which is returned by call() fucntion .
when i called function p() . it has not any return statement , but autometicaly return value of call() fcuntion .
int call(int x)
{
return x;
}
int p(int k)
{
call(4);
}
void solve()
{
cout<<p(9)<<endl; //output is x
}
int main()
{
solve();
}
This question already has answers here:
How can I pass std::unique_ptr into a function
(8 answers)
Closed 3 years ago.
How do I pass the unique_ptr in the below code? This code does not compile.
int abc(int*& a)
{
return 1;
}
int main()
{
std::cout<<"Hello World";
std::unique_ptr<int> a(new int);
abc(a.get());
return 0;
}
You may pass it by const reference or reference for example:
int abc(const std::unique_ptr<int>& a)
This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 4 years ago.
I try to give a reference to a member function (func) to a function (Multiprotocol) and call it with an object (z) of this class.
But i have problems with the line
result = z.*f(std::forward<Args>(args)...));
I tried things like
z.(*f) (std::forward<Args>(args)...))
and
z.(*(&f)(std::forward<Args>(args)...));
But i don't knwow how to this. Can anybody help me?
class test
{
public:
static int func()
{
std::cout << "in func";
}
};
class MultiProtocol
{
public:
template<typename Function, typename... Args>
bool functionImpl(Function f, Args&&... args)
{
int result = 0;
result = z.*f(std::forward<Args>(args)...));
return result;
}
private:
test z;
};
Main is:
int main()
{
MultiProtocol rr;
rr.functionImpl(&test::func);
}
Nearly, it is:
(z.*f)(std::forward<Args>(args)...)
Cannot be
z.(*f) (std::forward<Args>(args)...))
z.(*(&f)(std::forward<Args>(args)...));
as we should use operator .* (or ->*).
z.*f(std::forward<Args>(args)...) would be valid if f(args...) would return int test::* (pointer on member) as it is actually z .* (f(std::forward<Args>(args)...)).
This question already has answers here:
Function pointer vs Function reference
(4 answers)
Closed 6 years ago.
int g()
{
return 0;
}
int f1(int(fn)())
{
return fn();
}
int f2(int(*fn)())
{
return fn();
}
int main()
{
f1(g);
f2(g);
}
The code above can be sucessfully compiled.
I just wonder:
What's the difference between int f(int(fn)()) and int f(int(*fn)())?
Why does both of them are legal in C++?
int f(int(fn)()) and int f(int(*fn)()) are the same thing. A function takes a function pointer as parameter.
When we pass the name of a function as parameter, the function name is automatically converted to a pointer. So
int f(int(fn)()) {}
int f(int(*fn)()) {} // redefinition error
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(); }