Concept check for pointer
Does anyone know of a concept check in C++ that checks if a template parameter is a pointer to an object or an object?
I would have thought:
would do the trick.
#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.
If you''re using the Boost libraries, then you may aswell use their concept checks. See boost/type_traits for details.
..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 };};
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.
So if I pass a typedef (eg charPtr for char*), that will go through the <T*> template and not the generic <T> template? If so, why? And what about smart pointers?
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
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
"take a look around" - limp bizkit
www.google.com
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)]
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement