Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the type of the return value in C++

Suppose we have a function f which returns a value of some unknown type (let's call it T) and takes a value of the type T as an argument (and possibly has some other arguments). How do I get the return type of f in C++14?

There is a way to do it if we know the know the argument types (via std::result_of). Is it possible if we know all the argument types except T?

Example:

template <class F> // F is functor with   T operator()(T a, T b)
class A {
    // Here I want to do
    // T some_function(T some_arg) { ... }
}
like image 340
DLunin Avatar asked Oct 05 '14 10:10

DLunin


People also ask

What does typeof () in C return?

In other languages, such as C# or D and, to some degree, in C (as part of nonstandard extensions and proposed standard revisions), the typeof operator returns the static type of the operand. That is, it evaluates to the declared type at that instant in the program, irrespective of its original form.

How do you determine return type?

The getReturnType() method of Method class Every Method has a return type whether it is void, int, double, string or any other datatype. The getReturnType() method of Method class returns a Class object that represent the return type, declared in method at time of creating the method.

What is the type of the return value?

A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.

What is the return value of get C?

The getc() function It returns the character that was read in the form of an integer or EOF if an error occurs.


1 Answers

template <typename T>
struct return_type;

template <typename R, typename... Args>
struct return_type<R(Args...)> { using type = R; };

template <typename R, typename... Args>
struct return_type<R(*)(Args...)> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...)> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) &> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) &&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) volatile> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) volatile&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) volatile&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const volatile> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const volatile&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const volatile&&> { using type = R; };

template <typename T>
using return_type_t = typename return_type<T>::type;

Test:

#include <type_traits>

struct Functor
{
    int operator()(int i, int j) { return i + j; }
};

template <class F>
struct A
{
    using T = return_type_t<decltype(&F::operator())>;

    T some_function(T some_arg) { return some_arg; }
};

int main()
{
    A<Functor> a;
    static_assert(std::is_same<decltype(a.some_function(1)), int>::value, "!");
}

DEMO

like image 77
Piotr Skotnicki Avatar answered Sep 29 '22 11:09

Piotr Skotnicki