Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

schiggl

Template Typ *

This topic is 5893 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

I want to make a template class, that only accept Pointers like:
MyClass < int * >, MyClass < char * >, ...
  
and NOT:
MyClass < int > or MyClass < char > or ....
  
How can i do this? If i'm able to. [edited by - schiggl on June 4, 2002 5:07:22 AM]

Share this post


Link to post
Share on other sites
Advertisement

  
template<typename T> class MyClass;
// No implementation for ''generic'' case


template<typename T> class MyClass<T*>
{
// Implementation for pointer case

};


Will not work with VC++ which does not support partial template specialisation.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Actually, here''s using the ''usual'' VC++ workaround.
Get the Boost library for type_traits to get the ''is_pointer'' template (makes life easier).


  
#include <boost/type_traits.hpp>


struct MyClassPointer
// This struct holds the specialized implementation

// for pointer types.

// MyClassPointer has no template parameter because

// we would need partial template specialisation...

// and this is what we are trying to avoid.

// Instead, it has a templated inner class.

{
template<class T> class Implementation
{
public:
void Foo()
{
// do something

};
};
};

template<bool IsPtr> struct MyClassDispatch;
// No implementation of the general case


template<> struct MyClassDispatch<true>
// Redirect to MyClassPointer in the pointer case

{
typedef MyClassPointer Type;
};


template<class T> class MyClass
{
public:
inline void Foo()
{
typedef typename MyClassDispatch<boost::is_pointer<T>::value>::Type Type;
// select a specialisation implementation

Type::Implementation<T>::Foo();
// reapply the template parameter T and call the ''right'' Foo()

};
};


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!