int myfun() { return 42; }
I know I can write
auto myvar = myfun();
but what if I just want to declare myvar
(without using a common typedef
)?
the_type_returned_by_myfun myvar;
What can be written instead of the_type_returned_by_myfun
?
In C, all functions must be written to return a specific TYPE of information and to take in specific types of data (parameters). This information is communicated to the compiler via a function prototype. The function prototype is also used at the beginning of the code for the function.
What happens when you call a function with return value without assigning it to any variable? The return value of a function need not be used or assigned. It is ignored (usually quietly). The function still executes and its side effects still occur.
If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.
Yes any function can return a function pointer.
You can use decltype
.
decltype(myfun()) myvar; // or typedef decltype(myfun()) myfun_ret; myfun_ret myvar2;
And if the function happens to have parameters, you can produce fake parameters with std::declval
.
#include <utility> int my_other_fun(foo f); typedef decltype(myfun(std::declval<foo>())) my_other_fun;
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