I want something like this:
std::tuple<int, bool, double> MyFunction_1 (void); void MyFunction_2 (decltype (MyFunction_1) ¶ms);
Obviously, in this example a code pointer to function would be passed.
I want to have the equivalent of this:
void MyFunction_2 (std::tuple<int, bool, double> ¶ms);
Is it possible to do so?
So yes there are stack or local variables to a specified scope resolution.
Till now, we have seen that in C programming, we can pass the variables as an argument to a function. We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer.
A variable defined inside a function. A local variable can only be used inside its function. parameter. A name used inside a function to refer to the value passed as an argument.
Yes, you can. In Javascript functions can be passed as an argument to an another function and functions can also be returned from a fuction. Such type of functions are called Higher Order functions.
decltype (MyFunction_1)
will give you the type of MyFunction_1
(i.e. the function type std::tuple<int, bool, double> ()
), you need to emulate a function calling 1 (via adding ()
) to get the return type (i.e. std::tuple<int, bool, double>
), e.g.
void MyFunction_2 (decltype (MyFunction_1()) ¶ms); // ^^
1 The expression is evaluated at compile-time, the function won't be called at run-time actually.
The type of MyFunction_1
is not std::tuple<int, bool, double>
- informally you can think of it as a function pointer. In fact &MyFunction_1
decays to itself and is certainly a pointer.
So decltype(MyFunction_1)
is not what you want.
The solution is to write decltype(MyFunction_1())
. The type of MyFunction_1()
is std::tuple<int, bool, double>
. Note that this doesn't actually call the function; it's rather like sizeof
in that respect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With