Template Partial Specialization of Templated Type With Pointers

Started by
1 comment, last by Kyall 11 years, 6 months ago
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.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Yeah, that's pretty much what I figured.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

This topic is closed to new replies.

Advertisement