Is there a way to find what the template parameters are?

Hi,

For any template class X, is there a way to find the actual template parameters?

Like:

1
2
3
4
template<typename T, typename U>
struct X
{
};


Thanks!

Juan
Typically, X would expose them as a public member typedef (if the author of X believes there's a reason to make them accessible)
1
2
3
4
5
6
7
8
9
10
template<typename T, typename U>
struct X
{
    using type1 = T;
    using type2 = U;
};
...
using some_specific_x = X<int, double>;
...
using actual_template_parameter = some_specific_x::type1;
Last edited on
ok, I see.

Is there no "introspection" into the type (I mean the compiler knows the actual template parameters, is there no way to access them to see their "values" - types).

Seems to me this could be something for boost????


Regards,
Juan
well, if you know the shape of the template, you can pattern-match out those parameter types:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<typename T, typename U>
struct X
{
};

template<class> struct x_type_extractor;
template<class P1, class P2> struct x_type_extractor<X<P1, P2>> {
    using type1 = P1;
    using type2 = P2;
};

using some_specific_x = X<int, double>;

using actual_template_parameter = x_type_extractor<some_specific_x>::type1;
Last edited on
OP, perhaps std::enable_if and other stuff in <type_traits> is what you're looking for?
Thanks Cubby, yes I was aware of that kind of "extraction".
More generally though, helios suggests std::enable_if --- can one query the actual template types?
You can query properties about the types, such as whether it's an integer, whether it's a subtype of another class, stuff like that. To perform exact type comparisons you'll need to use template specialization. E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Foo;

template <typename T>
struct is_Foo{
    const bool value = false;
};

template <>
struct is_Foo<Foo>{
    const bool value = true;
};

template <typename T>
class Whatever{
public:
    void test(){
        //Note: This if evaluated at compile time. No comparison code is actually generated.
        if (is_Foo<T>::value)
            std::cout << "T == Foo\n";
        else
            std::cout << "T != Foo\n";
    }
    //If T == Foo, this function exists:
    std::enable_if<is_Foo<T>::value, void>::type do_something(){
        //...
    }
    //If T != Foo, this function exists:
    std::enable_if<!is_Foo<T>::value, void>::type do_something(){
        //...
    }
}
Thanks helios. However, I do know about enable_if and type_traits - but I don't think there are metafunctions for accessing actual template parameters. Am I wrong?

Why don't you just tell us what you want to do exactly? "Accessing actual template parameters" is too vague.
It is vague because my question aims at being very general, at answering questions like:

for any template class X, how can we obtain its actual template parameters? Just like we have std::is_const to retrieve information about a certain type, are there other metafunctions to obtain information about the template parameters being used in a specific instantiation?

I am sorry if its vague - but its in the nature of the question!`

Thanks
Juan
It is vague because my question aims at being very general
Can't you at least provide an example of a problem you'd like to solve?

for any template class X, how can we obtain its actual template parameters?
What do you mean by "actual" template parameters? Are you referring to an analogy of actual and formal parameters?

Just like we have std::is_const to retrieve information about a certain type, are there other metafunctions to obtain information about the template parameters being used in a specific instantiation?
Any of the classes in type_traits can be used with template parameters of metatype typename.
The type traits in type_traits are what they are. If you want to query for other traits, it may or may not be possible, depending on what you're looking for. For example, the trait type_name_contains_the_letter_A<T> is not doable except by exhaustive enumeration of all types in the source.
Last edited on
Topic archived. No new replies allowed.