I have a template class A
template<template<class> class E, class N>
class A
{}
and I also have a template function that takes A
as an argument:
template<class T>
auto make_something(const T& t){}
used like make_something(a)
on an object a
of A
type.
I would like to be able to tell what is the type N
from inside the make_something
function. Is there any way of achieving this without changing the template argument of make_something
?
CodePudding user response:
(In the following I've changed template<class> class E
to template<class, class> class E
because I used std::vector
, which has two type template parameters, for E
.)
I have no idea what you're doing, but to ask question about types, you need to write some traits.
I guess something like the following could be a starting point:
// to ask if something is an A at all
template<typename T>
struct IsAnA : std::false_type {};
template<template<class, class> class E, class N>
struct IsAnA<A<E, N>> : std::true_type {};
template<typename T>
constexpr bool is_an_A_v = IsAnA<T>::value;
// to query A's second template arg
template<typename T>
struct SecondTemplArgOfA {};
template<template<class, class> class E, class N>
struct SecondTemplArgOfA<A<E, N>> {
using type = N;
};
template<typename T>
using second_templ_arg_of_A_t = typename SecondTemplArgOfA<T>::type;
// make use of it
template<class T>
auto make_something(const T& t){
if constexpr (is_an_A_v<T>) {
using its2ndTemplArg = second_templ_arg_of_A_t<T>;
// so what?
} else {
// you've fed make_something with a non-A, so
// you can't get its second template arg
}
}