mardi 1 septembre 2015

c++11: void in template arguments

I was implementing some stuff in C++, and I came across a situation, I did not know how to handle properly. Consider following example

template<typename F>
struct function_wrapper;

template<typename Return, typename... Input>
struct function_wrapper<Return(Input...)>
{
    function_wrapper(/*...*/) = /* ... */
    std::tuple<Input...> input_args;
    Return return_value;

    void first_calculate_result() { return_value = somehow_manipulate(input_args); }
    Return operator()() { return return_value; }
}

Now, obvious usage is like this

my_function<int(double)> stored_fnc(5.5);
stored_fnc.first_calculate_result();
/* something maybe else */
std::cout << "Result is: " << stored_fnc() << std::endl;

Everything works as expected, as long as function's return type is not void.

My question is, do I have to write partial specification for my_function<void(Input...)>, where I would omit all Return stuff, or is there an easier way? Moreover, why does the C++ standard does not allow me to define void variables?

Aucun commentaire:

Enregistrer un commentaire