Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template Partial Specialization of Templated Type With Pointers

Started by Kyall Oct 11, 2012 at 8:18 AM 1 replies 1k views
Original Post
Kyall
Kyall
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* and the non pointer reference for MyTemplate as well to manage this stuff and amazingly enough:

template void Info< MyTemplate >

Gives me a MyTemplate* when MyTemplate* is asked for, when I use MyTemplate 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* is unusable.


Only just realised after posting that the MyTemplate* 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!
wqking
wqking
MyTemplate* is a pointer to MyTemplate, so it's not handled by template struct Info > because the later is to handle non-pointer one.
It's expected.
To handle MyTemplate*
You must write another specialization,
template
struct Info *>

Hope I understood your question because your text was very fuzzy and hard to understand to me.
Kyall
Kyall
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.