Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the return type of the function be obtained from within the function?

Can the return type of a function be obtained in a simple way within the function?

For example, given:

template <typename P>
static inline auto foo(P p) -> typename std::remove_reference<decltype(*p)>::type {
    typename std::remove_reference<decltype(*p)>::type f{};  // <-- here

    ...
}

In C++11 can I refer to the big nasty return type of foo, within foo itself, without repeating it, at the line marked // <-- here?

like image 574
BeeOnRope Avatar asked Sep 05 '18 17:09

BeeOnRope


People also ask

How do you determine the return type of a function?

function myFunction(a: number, b: number): void { console. log(a); console. log(b); } // 👇️ type T = void type T = ReturnType<typeof myFunction>; If the function might return values of multiple types, its return type will be a union type.

Can we return a function inside a function?

A function is an instance of the Object type. You can store the function in a variable. You can pass the function as a parameter to another function. You can return the function from a function.

Can the return type of the main function be inter?

Explanation: True, The default return type for a function is int.

Which function returns values from a function?

A function that returns a value is called a value-returning function. A function is value-returning if the return type is anything other than void . A value-returning function must return a value of that type (using a return statement), otherwise undefined behavior will result.


1 Answers

Call the function with a decltype.

decltype(foo(p)) f{};
like image 187
Rakete1111 Avatar answered Oct 18 '22 22:10

Rakete1111