English 中文(简体)
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.

最佳回答

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.

问题回答
template <class T> void extract_type(Holder<T>)
{
    printf("%s
", 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"

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 :-)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签