• Advertisement
Sign in to follow this  

Template Partial Specialization of Templated Type With Pointers

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok guys, this code works and I don't know why, some one enlighten me: (and yes in this case I need it to work this way and I find it strange that it does, I was expecting to craft a more complicated solution to this problem)


// This is all just test code

// ZE templates
template <typename T>
struct Info {
static void Print();
};
template <typename T>
struct Info<MyTemplate<T>> {
static void Print();
};
template <typename T>
void Info<T>::Print() {
std::cout << typeid( T ).name() << std::endl;
}
template <typename T>
void Info<MyTemplate<T>>::Print() {
std::cout << typeid(T).name() << std::endl;
std::cout << typeid( MyTemplate<T> ).name() << std::endl;
}

// ZE usage


Info<int>::Print();
// Output:
// int
Info<MyTemplate<int>>::Print();
// Output:
// int
// class MyTemplate<int>
Info<MyTemplate<int*>*>::Print();
// Output: class MyTemplate<int*>*



I needed to get that pointer reference for MyTemplate<int>* and the non pointer reference for MyTemplate<int> as well to manage this stuff and amazingly enough:

template <typename T> void Info< MyTemplate<T> >

Gives me a MyTemplate<T>* when MyTemplate<T>* is asked for, when I use MyTemplate<T> in the template code. Sort of hard to understand that since it's a bit of a linguistic mess, but I'm pretty sure you guys get the point. It seems the T of the template argument is lost in the second case, and I don't know why that is either. Full disclosure; I am running on the belief that this is functioning by typeid working, as of yet I haven't tested whether or not the lack of the T in the second MyTemplate means that the MyTemplate<T>* is unusable.


Only just realised after posting that the MyTemplate<int>* case is not being handled by the partially specialized function. So it's not working, mod delete.

basically the end result is that there will have to be two partial specializations for pointer & non pointer type. Edited by Kyall

Share this post


Link to post
Share on other sites
Advertisement
MyTemplate<int*>* is a pointer to MyTemplate<int*>, so it's not handled by template <typename T> struct Info<MyTemplate<T> > because the later is to handle non-pointer one.
It's expected.
To handle MyTemplate<int*>*
You must write another specialization,
template <typename T>
struct Info<MyTemplate<T> *>

Hope I understood your question because your text was very fuzzy and hard to understand to me. Edited by wqking

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement