Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Template Partial Specialization of Templated Type With Pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Kyall   Members   -  Reputation: 287

Like
0Likes
Like

Posted 11 October 2012 - 02:18 AM

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, 11 October 2012 - 02:21 AM.

I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

Sponsor:

#2 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 11 October 2012 - 02:28 AM

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, 11 October 2012 - 02:28 AM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#3 Kyall   Members   -  Reputation: 287

Like
0Likes
Like

Posted 11 October 2012 - 07:04 PM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS