In C++, is it possible to get the return type of a function in order to declare a variable without calling that function?

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?

How to declare a C function with an undetermined return type?

Can I declare a C function with an undetermined return type (without C compiler warning)? The return type could be int, float, double, void *, etc. undetermined_return_type miscellaneousFunction(undet

Possible to declare function of enumerated type?

Just wondering if it is possible to declare a function of an enumerated type in C++ For example: class myclass{ //…. enum myenum{ a, b, c, d}; myenum function(); //…. }; myenum function() { //….

Return enum value without calling get function

Is it possible in Java to return the enum value without having to call a function to return the value, such as getFlag() in my example? If so, how? public enum MessageFlags { BIT0((short)1), BIT1((sho

Is it possible to get the return type of a function without passing arguments to it?

Obviously, you can get the return type of a function with decltype(foo()), but if foo takes arguments that won’t work, you’d have to pass some dummy arguments to foo in order for it to work. But, is t

Declare variable whose type is a function’s return type

I’m currently using a type alias: type FooType = Int val foo = (_: Int) * 2 def takeFooRet(x: FooType) = … however, I’d like to do something like: val foo = (_: Int) * 2 def takeFooRate(x: foo.RetT

Extract the return type of a function without calling it (using templates?)

I’m looking for a way in C++ to extract the return type of a function (without calling it). I presume this will require some template magic. float Foo(); int Bar(); magic_template<Foo>::type var

Why is it not possible to declare a function with VAR return type?

In C#, we have var data type but we can’t use it as functions return type. Why this is not possible? public var myFunction() { var = some operations }

In C, declare and initialize a variable in a small function or just return a response

Coming from a PHP background, I’m used to writing small functions that return a string (or the response from another function) like so: function get_something(){ return foo; } However, I’m new to C

In C++, how can I get an arbitrary function’s type from its declaration? [duplicate]

Possible Duplicate: Extract the return type of a function without calling it (using templates?) Starting with this (provided by somebody else): int my_function(int, int *, double); I want to get to

Function without return type

In my project (which I inherited from someone) there are a lot of functions like: Public Function DoSomething(ByVal s As String) ‘ Do something to public properties End Function And they are called l

Answers

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;

This is the job of decltype:

decltype(myfun()) myvar;

decltype is your friend:

decltype(myfun()) myvar;