Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 503
drpepper Avatar asked Dec 09 '22 18:12

drpepper


1 Answers

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.

like image 139
Sebastian Avatar answered Mar 29 '23 23:03

Sebastian