Containers for different signature functions

白昼怎懂夜的黑 提交于 2019-12-01 20:23:28

How about something like this:

template <typename result_t, typename... args_t>
class Memoizer
{
public:
    typedef result_t (*function_t)(args_t...);
    Memoizer(function_t func) : m_func(func) {}

    result_t operator() (args_t... args)
    {
        auto args_tuple = make_tuple(args...);

        auto it = m_results.find(args_tuple);
        if (it != m_results.end())
            return it->second;

        result_t result = m_func(args...);
        m_results.insert(make_pair(args_tuple, result));
        return result;
    }

protected:
    function_t m_func;
    map<tuple<args_t...>, result_t> m_results;
};

Usage is like this:

// could create make_memoizer like make_tuple to eliminate the template arguments
Memoizer<double, double> memo(fabs);
cout << memo(-123.456);
cout << memo(-123.456); // not recomputed

It's pretty hard to guess at how you're planning to use the functions, with or without memoisation, but for the container-of-various-function<>s aspect you just need a common base class:

#include <iostream>
#include <vector>
#include <functional>

struct Any_Function
{
    virtual ~Any_Function() {}
};

template <typename Ret, typename... Args>
struct Function : Any_Function, std::function<Ret(Args...)>
{
    template <typename T>
    Function(T& f)
      : std::function<Ret(Args...)>(f)
    { }
};

int main()
{
    std::vector<Any_Function*> fun_vect;
    auto* p = new Function<int, double, double, int> { [](double i, double j, int z) {
        return int(i + j + z);
    } };
    fun_vect.push_back(p);
}
svick

The problem with this is how to make it type-safe. Look at this code:

MultiMemoizator mm;
std::string name = "identity";
mm.addFunction(name, identity);
auto result = mm.callFunction(name, 1);

Is the last line correct? Does callFunction have the right number of parameters with the right types? And what is the return type?

The compiler has no way to know that: it has no way of understanding that name is "identity" and even if it did, no way to associate that with the type of the function. And this is not specific to C++, any statically-typed language is going to have the same problem.

One solution (which is basically the one given in Tony D's answer) is to tell the compiler the function signature when you call the function. And if you say it wrong, a runtime error occurs. That could look something like this (you only need to explicitly specify the return type, since the number and type of parameters is inferred):

auto result = mm.callFunction<int>(name, 1);

But this is inelegant and error-prone.

Depending on your exact requirements, what might work better is to use "smart" keys, instead of strings: the key has the function signature embedded in its type, so you don't have to worry about specifying it correctly. That could look something like:

Key<int(int)> identityKey;
mm.addFunction(identityKey, identity);
auto result = mm.callFunction(identityKey, 1);

This way, the types are checked at compile time (both for addFunction and callFunction), which should give you exactly what you want.

I haven't actually implemented this in C++, but I don't see any reason why it should be hard or impossible. Especially since doing something very similar in C# is simple.

you can use vector of functions with signature like void someFunction(void *r, ...) where r is a pointer to result and ... is variadic argument list. Warning: unpacking argument list is really inconvenient and looks more like a hack.

At first glance, how about defining a type that has template arguments that differ for each function, i.e.:

template <class RetType, class ArgType> 
class AbstractFunction {
    //etc.
}

have the AbstractFunction take a function pointer to the functions f1-f5 with template specializations different for each function. You can then have a generic run_memoized() function, either as a member function of AbstractFunction or a templated function that takes an AbstractFunction as an argument and maintains a memo as it runs it.

The hardest part will be if the functions f1-f5 have more than one argument, in which case you'll need to do some funky things with arglists as template parameters but I think C++14 has some features that might make this possible. An alternative is to rewrite f1-f5 so that they all take a single struct as an argument rather than multiple arguments.

EDIT: Having seen your problem 1, the problem you're running into is that you want to have a data structure whose values are memoized functions, each of which could have different arguments.

I, personally, would solve this just by making the data structure use void* to represent the individual memoized functions, and then in the callFunction() method use an unsafe type cast from void* to the templated MemoizedFunction type you need (you may need to allocate MemoizedFunctions with the "new" operator so that you can convert them to and from void*s.)

If the lack of type safety here irks you, good for you, in that case it may be a reasonable option just to make hand-written helper methods for each of f1-f5 and have callFunction() dispatch one of those functions based on the input string. This will let you use compile-time type checking.

EDIT #2: If you are going to use this approach, you need to change the API for callFunction() slightly so that callFunction has template args matching the return and argument types of the function, for example:

int result = callFunction<int, arglist(double, float)>("double_and_float_to_int", 3.5, 4);

and if the user of this API ever types the argument type or return types incorrectly when using callFunction... pray for their soul because things will explode in very ugly ways.

EDIT #3: You can to some extent do the type checking you need at runtime using std::type_info and storing the typeid() of the argument type and return type in your MemoizedFunction so that you can check whether the template arguments in callFunction() are correct before calling - so you can prevent the explosion above. But this will add a bit of overhead every time you call the function (you could wrap this in a IF_DEBUG_MODE macro to only add this overhead during testing and not in production.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!