mercredi 4 mars 2015

Extract template class default parameters

Is there a way to extract a template class' default parameters only knowing the unspecialized template class at compile time?


I know how to extract an instantiated template class' parameters, like this:



// Just an example class for the demonstration
template<class A, class B=void>
struct example {};

// Template parameters storage class
template<class...An>
struct args;

// MPL utility that extracts the template arguments from a template class
template<class C>
struct get_args
{
typedef args<> type;
};
template<template<class...> class C, class...An>
struct get_args< C<An...> >
{
typedef args<An...> type;
};

// And the assertion
static_assert(
std::is_same
< args<int,void>,
get_args< example<int> >::type
>::value,
"Check this out"
);


Now what I would like to know is if decltype or anything else could be used to retrieve the default template parameters only from the unspecialized template class:



// MPL utility that extract template default arguments from a template class
template<template<class...> class C>
struct get_default_args
{
typedef /* what goes here? */ type;
};

// And the assertion
static_assert(
std::is_same
< args<void>,
get_default_args< example >::type
>::value,
"Check this out"
);


For now, I only figured out how to extract the number of parameters of a template class, not their default value:



namespace detail {
template< template<class> class >
boost::mpl::size_t<1> deduce_nb_args();
template< template<class,class> class >
boost::mpl::size_t<2> deduce_nb_args();
template< template<class,class,class> class >
boost::mpl::size_t<3> deduce_nb_args();
/* ... and so on ... */
}

// MPL utility that extract the number of template arguments of a template class
template<template<class...> class C>
struct get_nb_args :
decltype(detail::deduce_nb_args<C>()) {};

// And the assertion
static_assert(
get_nb_args< example >::value == 2,
"Check this out"
);

Aucun commentaire:

Enregistrer un commentaire