The output of the return type of a function or functor in C ++

I need to use the return value of a function / functor without knowing what type it is (i.e. as a template).

So far, I could pass it to the second function without problems:

template <typename T>
void DoSomething(T value);

...

DoSomething(FunctionWhoseReturnedTypeIsUnknown(...));

I want to use the inline return value (without having to call the second function):

WhatGoesHere? x=FunctionWhoseReturnedTypeIsUnknown(...);

Two methods seem conceptually identical to me (generic-programming-wize), but can the latter be achieved in C ++?

+3
source share
5 answers

Not yet. In C ++ 0X you can use autohow WhatGoesHere. Experimental support already exists for some compilers (e.g. gcc 4.4).

+4
source

, auto , , typedef, result_type. . "" (pointer_to_unary_function/pointer_to_binary_function -). , boost library .

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>

int answer()
{
    return 42;
}

template <class T>
T unknown()
{
    return T();
}

template <class Function>
void foobar(Function unknown)
{
    typename Function::result_type x = unknown();
    std::cout << x << '\n';
}

int main()
{
    foobar(boost::function<int()>(answer));
    foobar(boost::bind(unknown<int>));
}

, pointer_to_zeronary_function. ( , , , ptr_fun, ​​ , (?)

template <class T>
class pointer_to_zeronary_function
{
    typedef T(*zeronary_func)();
    zeronary_func func;
public:
    typedef T result_type;
    pointer_to_zeronary_function(zeronary_func f): func(f) {}
    T operator()() const
    {
        return func();
    }
};

template <class T>
pointer_to_zeronary_function<T> ptr_fun(T(*f)())
{
    return pointer_to_zeronary_function<T>(f);
}

...
//usage:
foobar(ptr_fun(answer));
+5

, boost:: any :

#include <iostream>
#include <boost/any.hpp>

namespace {
  template <class T>
  T return_arg(T t) {
    return t;
  }
}

int main() {
  try {  
    boost::any i = return_arg(1);
    boost::any s = return_arg("a string");
    std::cout << boost::any_cast<int>(i) << " "
          << boost::any_cast<const char*>(s)
          << std::endl;
  }
  catch(const boost::bad_any_cast &) {
      return 1;
  }
}
+2

++ 0x , auto.

auto x = SomeFunction();

# var: , .

++ 0x . , ++ 0x .

+1

, , , . , , ++ 11 (aka ++ 0x) Boost, , , , , .

UncleBens, STL , ++ 11 Boost: http://www.cplusplus.com/reference/std/functional/

, . , , .

( , ) stl, . , - / :

#include <iostream>
#include <functional>

using namespace std;


// Simple function [pointer] that adds one to its argument
int addOne(int n)
{
    return n + 1;
}

// Simple functor that multiplies its argument by two
class timesTwo
{
    public:
    int operator()(int n) const { return n * 2; }
};


// Simple higher-order function: takes a functor f and calls f on n, returning the result
// This is your template function in which you want to know the return type of f
template <typename Functor>
void printResultImpl(Functor f, typename Functor::argument_type n)
{
    typename Functor::result_type r = f(n);
    cout << r << endl;
}


// Wrapper function for function pointer
template <typename Arg, typename Result>
void printResult(Result (*f)(Arg), Arg n)
{
    printResultImpl(ptr_fun(f), n);
}

// Wrapper function for functor (function object)
template <typename Functor, typename Arg>
void printResult(Functor f, Arg n)
{
    printResultImpl(bind1st(mem_fun(&Functor::operator()), &f), n);
}


// Prints out 8 then 14
int main()
{
    printResult(addOne, 7);
    printResult(timesTwo(), 7);
}

There are several restrictions for this method: 1. You cannot return your function for a result of a functor type (since the wrapper function does not know the type of result) 2. It relies on unary_function or binary_function in stl. As UncleBens demonstrated, you can expand to other types - just follow the ad template at: http://www.cplusplus.com/reference/std/functional/

But he worked for what I needed; maybe this will work for someone else.

+1
source

Source: https://habr.com/ru/post/1715238/


All Articles