drpepper
drpepper

Reputation: 462

Can I extract C++ template arguments out of a template class?

Basically, given a template class like this:

template< class Value > class Holder { }; 

I would like to be able to discover the type Value for a given Holder class. I thought that I would be able to make a simple metafunction that takes a template template argument, like this:

template< template< class Value > class Holder > class GetValue
{
    typedef Value Value;
};

And then extract out the Value type like this:

GetValue< Holder< int > >::Value value;

But instead I just get an error message pointing to the metafunction declaration:

error: ‘Value’ does not name a type

Is there any way to accomplish this kind of thing? Thanks.

[EDIT] I also get the error messages:

error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class Value> class Holder> class GetValue’
error:   expected a class template, got ‘Holder<int>’

Which leads me to conclude that Phil Nash is right, you can't pass a class as a template template argument.

Upvotes: 4

Views: 3941

Answers (3)

philsquared
philsquared

Reputation: 22493

I think you need a typename in there somewhere.

[edit] Damn, was just going to check my suspicion that it needed to be before the GetValue, but Mykola got there first :-)

The issue is that Value is a dependent type. The compiler doesn't know whether it is meant to name a type or a value, so you need to provide the hint.

[edit2]

Oops. There is a problem with trying to answer too fast. I missed the fact that you were trying to use a template template to achieve this. What that allows you to do is to pass a template in instead of a type. In this case you can pass a Holder. But that doesn't help you because you want to pass a type, Holder<int>.

Looks like Mykola realised this too and removed his answer.

You'll need to use Partial Template Specialization for this. Before I had a chance to complete my own example Sebastian has beaten me to it :-)

Upvotes: 1

MSN
MSN

Reputation: 54554

template <class T> void extract_type(Holder<T>)
{
    printf("%s\n", typeid(T).name());
}

I'm assuming though that you want to use that in a non-templated function. That's a bit more difficult:

template <class T> class GetValue;
template <class T> class GetValue<Holder<T> >
{
public:
    typedef T value_type;
};

The magic google words are "partial template specialization"

Upvotes: 2

Sebastian
Sebastian

Reputation: 4950

Why don't you simply change your holder class to

template< class Value > class Holder {
    typedef Value value_type;

    value_type m_val; // member variable
};

In any method that consumes an object of type Holder< T > you can access the contained type like that:

template< class THolder >
void SomeMethod( THolder const& holder ) {
     typename THolder::value_type v = holder.m_val;
}

This approach follows the pattern all STL classes use, e.g., std::vector< int >::value_type is int.

I think you're trying to do partial template specialization:

template<class T>
class GetValue {
};

template<class Value>
class GetValue< Holder<Value> > {
public:
    typedef Value value_type;
};

In your code, you could then do the following:

template<class THolder>
void SomeMethod( THolder const& h ) {
    typename GetValue< THolder >::value_type v = h.m_v;
}

In general, I'd prefer the first solution though.

Upvotes: 11

Related Questions