I'm following the sample templates within the book 'Practical C++ Metaprogramming' and have reached a part of the sample where I cannot get the code to compile without bypassing the alias. When using the alias make_tuple_of_derefed_params_t I receive the compiler error 'implicit instantiation of undefined template' when it has been defined. I can call it directly using the partial template specialization make_tuple_of_derefed_params but not with the alias. Is there something else I need to do? I receive the error in both 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;
The full code is:
#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;
}
make_tuple_of_derefed_params
is only defined 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 for when 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>...>;
};