Implicitly creating an undefined template when creating a partial template specification alias

I follow the sample patterns in the book Practical C ++ Metaprogramming and have reached a part of the sample where I cannot compile the code without traversing the alias. When using the make_tuple_of_derefed_params_t alias , I get an implicit compiler template "undefined instance" when it was defined. I can call it directly using the make_tuple_of_derefed_params partial template specialization , but not with the alias. Is there anything else I need to do? I get an error like in clang ++ and g ++.

 template <typename F>
    class make_tuple_of_derefed_params;

template <typename Ret, typename... Args>
    struct make_tuple_of_derefed_params<Ret (Args...)>
{
    using type = std::tuple<std::remove_pointer_t<Args>...>;
};

template <typename F>
using make_tuple_of_derefed_params_t = typename make_tuple_of_derefed_params<F>::type;

      

Complete code:

#include <numeric>
#include <iostream>
#include <type_traits>
#include <tuple>
#include <utility>
#include <time.h>

void adjust_values(double * alpha1,double * beta1,double * alpha2,double * beta2) { }

struct location {
    int x;
    int y;
};

class reading
{
    /* stuff */
    public:
    double alpha_value(location l, time_t t) const { return 1.5; }
    double beta_value(location l, time_t t) const { return 2.5; }
    /* other stuff */
};

 template <typename F>
    class make_tuple_of_derefed_params;

template <typename Ret, typename... Args>
    struct make_tuple_of_derefed_params<Ret (Args...)>
{
    using type = std::tuple<std::remove_pointer_t<Args>...>;
};

template <typename F>
using make_tuple_of_derefed_params_t = typename make_tuple_of_derefed_params<F>::type;

template <std::size_t FunctionIndex,typename FunctionsTuple,
        typename Params, std::size_t... I>
    auto dispatch_params(FunctionsTuple & functions,Params & params,
        std::index_sequence<I...>)
{
    return (std::get<FunctionIndex>(functions))(std::get<I>(params)...);
}

template <typename FunctionsTuple, std::size_t... I, typename Params,
            typename ParamsSeq>
    auto dispatch_functions(FunctionsTuple & functions,
            std::index_sequence<I...>, Params & params,
            ParamsSeq params_seq)
{
    return std::make_tuple(dispatch_params<I>(functions,params,params_seq)...);
}

template <typename LegacyFunction,typename... Functions,typename... Params>
    auto magic_wand(
        LegacyFunction legacy,
        const std::tuple<Functions...> & functions,
        const std::tuple<Params...> & params1,
        const std::tuple<Params...> & params2)
{
    static const std::size_t functions_count = sizeof...(Functions);
    static const std::size_t params_count = sizeof...(Params);

    make_tuple_of_derefed_params_t<LegacyFunction> params =

    std::tuple_cat(
        dispatch_functions(functions,
            std::make_index_sequence<functions_count>(),
            params1,
            std::make_index_sequence<params_count>()),
        dispatch_functions(functions,
            std::make_index_sequence<functions_count>(),
            params2,
            std::make_index_sequence<params_count>()));
    /* rest of the code */
    static constexpr auto t_count = 
        std::tuple_size<decltype(params)>::value;

    dispatch_to_c(  legacy, 
                    params,std::make_index_sequence<t_count>());
    return params;
}

template <typename Reading>
    std::tuple<double, double, double, double>
        get_adjusted_values(Reading & r,
            location l,
            time_t t1,
            time_t t2)
{
    return magic_wand(adjust_values,
                std::make_tuple(
                    [&r](location l, time_t t)
                    {
                        return r.alpha_value(l, t);
                    },
                    [&r](location l, time_t t)
                    {
                        return r.beta_value(l, t);
                    }),
                std::make_tuple(l, t1),
                std::make_tuple(l, t2)
            );
}


int main()
{
    reading r;
    location l { 1,2 };
    time_t epoch = 0;
    time_t seconds = time(NULL);

    std::tuple<double, double, double, double> ret2 = 
        get_adjusted_values(r, l, epoch, seconds);

    return 0;
}

      

+3


source to share


2 answers


make_tuple_of_derefed_params

defined only for function types.

template <typename F>
class make_tuple_of_derefed_params;

// Definition here, note that it only defines type for "T(Ts...)" template parameters.
template <typename Ret, typename... Args>
struct make_tuple_of_derefed_params<Ret (Args...)>
{
    using type = std::tuple<std::remove_pointer_t<Args>...>;
};

template <typename F>
using make_tuple_of_derefed_params_t = typename make_tuple_of_derefed_params<F>::type;

// ...

//make_tuple_of_derefed_params_t<int> foo;         // Error if uncommented.
make_tuple_of_derefed_params_t<int(int)> bar;      // Works.
//make_tuple_of_derefed_params_t<int(*)(int)> baz; // Error if uncommented.

      



To fix this, you need to provide a definition for any other valid F

s. In this particular case, you need to provide a definition if F

is a function pointer.

template <typename Ret, typename... Args>
struct make_tuple_of_derefed_params<Ret (*)(Args...)>
{
    using type = std::tuple<std::remove_pointer_t<Args>...>;
};

      

+1


source


Considering

template <typename T> void f(T);
void g();

      



All three calls f<void()>(g)

, f<void(*)()>(g)

and are f<void(&)()>(g)

valid. Indeed, it f(g)

could be any of them. But the standard had to compel him, and he turned out f<void(*)()>(g)

. This means that when you then pass to T

another template, either that other template must deal with the types of function pointers, or you need to convert your function pointer type to a function type.

This is what happens to your challenge return magic_wand(adjust_values, ...)

.

+1


source







All Articles