Sign in to follow this  
DMINATOR

Is type a POD type ?

Recommended Posts

Hi I was reading lately allot information regarding memory management techniques and how to apply them to C++. I know that there are 2 different types POD and non POD. And the STL optimizes vector class or allocators to use the best algorithm for the right type. Somehow it knows what type is POD. So anyone knows how can I find if type is POD type ?

Share this post


Link to post
Share on other sites
There is no standard way to determine it. You have a couple of options though. If you know the compiler then most compilers have a way to determine whether a type is POD, but this isn't portable (this is usually what standard libraries do). A better approach for a normal programmer would be to use Boost.TypeTraits.is_pod which tries to determine if a type is POD (through specialization for different compilers and advanced techniques).

Share this post


Link to post
Share on other sites
The future standard will contain the current TR1, which defines a similar beast (std::tr1::is_pod<T>, which will become std::is_pod<T> IIRC). The VC++ .NET 2005 compiler contains a compiler intrisic which is named __is_pod(). And boost provide a mostly correct way to handle the situation.

Share this post


Link to post
Share on other sites
In GCC's includes, it is done like this: creates 2 structures __false_type and __true_type, which are typedef'd inside a templated structure, which is specialized to each of the POD types, so non specialized get to be non POD. Like this:


struct __true_type { };
struct __false_type { };

template <class T>
struct type_traits
{
typedef __false_type is_POD_type;
};

template <class T>
struct type_traits<T*>
{
typedef __true_type is_POD_type;
};

template <>
struct type_traits<char>
{
typedef __true_type is_POD_type;
};

// and so on...


// Then, you can define these functions to check if its POD:
inline bool check_POD_aux(__false_type)
{
return false;
}

inline bool check_POD_aux(__true_type)
{
return true;
}

template <class T>
inline bool check_POD()
{
return check_POD_aux(typename type_traits<T>::is_POD_type());
}


To make sure your code will compile everywhere, you can do this by yourself, since it's nice and easy and simple (AFAIK, every standard-compliant compiler would implement this to work the same way). Just make sure you change these type names so it won't conflict with anything when compiling with GCC.

Share this post


Link to post
Share on other sites
Thanks to everyone for the answers.

I guess there is not many options I have, I hope there would be some standard implementation in the future though.

Share this post


Link to post
Share on other sites
Quote:
Original post by DMINATOR
I guess there is not many options I have, I hope there would be some standard implementation in the future though.


It's implemented in TR1 so while it isn't a part of the 2003 C++ standard it's a part of a standard technical report. Whether it is standard just depends on what standard you refers to (2003 vs. 2003 with TR1). The problem is that all compilers have implemented the whole TR1 and some have a slightly different interface.

Actually a new version of the Boost library was released a couple of days ago and it includes a wrapper around the TR1 functionality provided by the compiler. If none is provided Boost uses its own implementation of TR1 which conforms to the 2003 standard. According to the documentation std::tr1::is_pod<T> provided by Boost.TR1 doesn't differ from the one in the technical report in any way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this