The following is an example from "Practical C++ Metaprogramming" (pages 16/17):
#include <tuple>
#include <typeinfo>
template <typename F>
struct make_tuple_of_params;
template <typename Ret, typename... Args>
struct make_tuple_of_params<Ret (Args...)>
{
using type = std::tuple<Args...>;
};
template <typename F>
using make_tuple_of_params_t = typename make_tuple_of_params<F>::type;
template<typename F>
void some_magic_function(F f)
{
// if F is in the form void(double*, double*)
// make_tuple_of_params is std::tuple<double*, double*>
make_tuple_of_params_t<F> params;
// ...
}
void Foo(double* x, double* y) { }
int main()
{
some_magic_function(Foo);
}
It's failing to compile:
$ clang++ -std=c++14 MakeTuple.cpp
MakeTuple.cpp:14:5: error: implicit instantiation of undefined template 'make_tuple_of_params<void (*)(double *, double*)>'
Is this because the non specialized version of make_tuple_of_params (lines 4 and 5 of the code above) is not defined?
You actually need to different overloading, depending if you extracting it from a pointer or from a signature template argument, see below.
template <typename F>
struct make_tuple_of_params;
template <typename Ret, typename... Args>
struct make_tuple_of_params<Ret (*)(Args...)> {
using type = std::tuple<Args...>;
};
template <typename Ret, typename... Args>
struct make_tuple_of_params<Ret(Args...)> {
using type = std::tuple<Args...>;
};
template <typename F>
using make_tuple_of_params_t = typename make_tuple_of_params<F>::type;
template <typename F>
bool some_magic_function(F f) {
// if F is in the form void(double*, double*)
// make_tuple_of_params is std::tuple<double*, double*>
return std::is_same<std::tuple<double*, double*>, make_tuple_of_params_t<F>>::value;
}
void Foo(double* x, double* y) {}
int main() {
cerr << some_magic_function(Foo) << endl;
cerr
<< std::is_same<std::tuple<int, int>, make_tuple_of_params_t<void(int, int)>>::value
<< endl;
// The latter one might be handy in some of template metaprogramming constructs
return 0;
}
Sorry, have not look at the book pages, so do not know what the authro meant there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.