Jump to content
  • Advertisement

Archived

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

dalleboy

Concept check for pointer

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

Advertisement
I would have thought:

  
#include <boost/static_assert.hpp>

template <typename T>
struct IsPointer
{
enum { value = false };
};
template <typename T>
struct IsPointer<T*>
{
enum { value = true };
};
// ...

template <typename T>
class MyClass
{
BOOST_STATIC_ASSERT(true == IsPointer<T>::value);
};


would do the trick.

Share this post


Link to post
Share on other sites
..and adding this will do the trick some more.


  
template<typename T> struct IsPointer<const T*>
{
enum { value = true };
};

template<typename T> struct IsPointer<T* const>
{
enum { value = true };
};

template<typename T> struct IsPointer<const T* const>
{
enum { value = true };
};

Share this post


Link to post
Share on other sites
Thank you all, I was searching for the wrong thing when I searched Boost. I thought that what I was looking for was called "concept checks", but what I really was looking for was "type traits". Thank you.

Share this post


Link to post
Share on other sites
smart pointers actually behave like objects, thats why they are smart (dont talk about auto_ptr, thats a stupid smart ptr:D)

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
So if I pass a typedef (eg charPtr for char*), that will go through the template and not the generic template?


Yes

quote:

If so, why?


Because it''s a typedef to a char*?

Typedef''s don''t create new types - struct, class, union, & enum do that.


Loki::TypeTraits::isPointer yields a compile-time constant bool.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
So if I pass a typedef (eg charPtr for char*), that will go through the template and not the generic template? If so, why?

Because it is a partial specialisation on type pointer-to-T, which the Standard allows.
quote:

And what about smart pointers?

They''ll hit the generic template, as they''re fully-fledged types, not pointer types.

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!