Connecting Tech Pros Worldwide Forums | Help | Site Map

Template for POD types only

Raider
Guest
 
Posts: n/a
#1: Dec 28 '05
Is it possible to create a template for POD types only?

I want to code something like this:

class Object // base class
{
virtual void WriteToStream(Stream &) = 0;
};

class Stream
{

template <typename TPOD>
inline void operator << (TPOD &POD)
{
RawWrite(&POD, sizeof(TPOD));
}

inline void operator << (Object &Obj)
{
Obj.WriteToStream(*this);
}

};

And I want compiler to use first method to write PODs and second to
write derived from Object (not Objects itself but derived from
Object!). Is it possible to tell compiler to use first method only for
PODs?


Luke Meyers
Guest
 
Posts: n/a
#2: Dec 28 '05

re: Template for POD types only


Are you saying you want the compiler to detect that a type is a POD
type, or do you intend all POD types to inherit from a common base
class? If that's all you want, just use partial template
specialization (with proper syntax -- go look it up) and you're all
set.

Luke

Axter
Guest
 
Posts: n/a
#3: Dec 28 '05

re: Template for POD types only



Raider wrote:[color=blue]
> Is it possible to create a template for POD types only?
>
> I want to code something like this:
>
> class Object // base class
> {
> virtual void WriteToStream(Stream &) = 0;
> };
>
> class Stream
> {
>
> template <typename TPOD>
> inline void operator << (TPOD &POD)
> {
> RawWrite(&POD, sizeof(TPOD));
> }
>
> inline void operator << (Object &Obj)
> {
> Obj.WriteToStream(*this);
> }
>
> };
>
> And I want compiler to use first method to write PODs and second to
> write derived from Object (not Objects itself but derived from
> Object!). Is it possible to tell compiler to use first method only for
> PODs?[/color]

Here's some example code using boost is_pod:

#include <iostream>
#include <string>

#include "boost\type_traits\is_pod.hpp"

using namespace std;

template <typename T, bool ISPOD = boost::is_pod<T>::value> struct
base_t {};

template <typename T> struct base_t<T,false>
{
void operator << (const T &Obj){ cout << "Is Not POD " << Obj <<
endl;}
};

template <typename T> struct base_t<T,true>
{
void operator << (const T &POD){cout << "Is POD " << POD << endl;}
};

class Stream
{
public:
template<typename T>
void operator << (const T &t)
{
base_t<T>() << t;
}
};

int main(int argc, char* argv[])
{
Stream My_Stream;
My_Stream << 123;

My_Stream << string("Hello");

Raider
Guest
 
Posts: n/a
#4: Dec 28 '05

re: Template for POD types only


Luke, I want the compiler to use RawWrite() for the first type and
SecondType::WriteToStream() for the second one:

enum FirstType
{
Bla, Bla2,
};

class SecondType : public Object
{
virtual void WriteToStream(Stream &);
}

....

Stream s;
FirstType o1;
SecondType o2;

s << o1;
s << o2;

Raider
Guest
 
Posts: n/a
#5: Dec 28 '05

re: Template for POD types only


Thanks Axter! I'll try to get boost and use your code.

Howard Hinnant
Guest
 
Posts: n/a
#6: Dec 28 '05

re: Template for POD types only


In article <1135762499.665506.117510@g47g2000cwa.googlegroups .com>,
"Raider" <sraider@yandex.ru> wrote:
[color=blue]
> Is it possible to create a template for POD types only?
>
> I want to code something like this:
>
> class Object // base class
> {
> virtual void WriteToStream(Stream &) = 0;
> };
>
> class Stream
> {
>
> template <typename TPOD>
> inline void operator << (TPOD &POD)
> {
> RawWrite(&POD, sizeof(TPOD));
> }
>
> inline void operator << (Object &Obj)
> {
> Obj.WriteToStream(*this);
> }
>
> };
>
> And I want compiler to use first method to write PODs and second to
> write derived from Object (not Objects itself but derived from
> Object!). Is it possible to tell compiler to use first method only for
> PODs?[/color]

Here's another approach (in addition to Axter's good suggestion). It
also requires boost:

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>

class Stream;

class Object // base class
{
public:
virtual void WriteToStream(Stream &) const = 0;
};

class Stream
{
public:
template <typename TPOD>
inline
typename boost::enable_if<boost::is_pod<TPOD> >::type
operator << (const TPOD &POD)
{
RawWrite(&POD, sizeof(TPOD));
}

inline void operator << (const Object &Obj)
{
Obj.WriteToStream(*this);
}

};

This approach saves you the trouble of having to work through an
implementation class (base_t in Axter's example). It more directly
reflects your goal of restricting the template parameter TPOD to just
pods, while allowing overloading to other operator<<.

-Howard
Luke Meyers
Guest
 
Posts: n/a
#7: Dec 28 '05

re: Template for POD types only


Okay, still a slight ambiguity, sorry. If I take you literally,
partial template specialization is still the answer -- it's certainly
sufficient for the example code you've posted. It's also sufficient if
you have a common "Object" base class for everything that isn't a POD.
If that wasn't what you meant, then I'd follow other people's
suggestion to use boost::is_pod<T>. The Boost libraries are of
phenomenal importance, so you should install them and learn to love
them regardless of whether you wind up needing them in this particular
case. Better yet, go see how is_pod is implemented. :)

I'll offer my own version of "how to do it with boost," though I must
apologize that I can't test this code myself right now as I'm in the
middle of a RedHat install, and attempting this with MSVC 6.0 sounds
like a cruel joke to play on myself and that dated compiler. Anyway,
this is the code I'd generate:

#include <string>
#include <boost/type_traits/is_pod.hpp> // <-- should use forward
slashes and angle brackets as here
#include <boost/static_assert.hpp>

class Stream {
private:
template <class T> class Stream & rawWrite(
friend template <class T, bool isPod> operator<<(Stream & os, T
obj);
};

template <class T, bool isPod>
Stream & operator<<(Stream & os, T nonPodObj) {
nonPodObj.writeToStream(os);
return os;
}

template <class T>
Stream & operator<< <T, true>(Stream & os, T pod) {
BOOST_STATIC_ASSERT(boost::is_pod<T>::value);
os.rawWrite(&pod, sizeof(T));
}

class Base {
public:
Stream & writeToStream(Stream & os);
};

/*** END CODE LISTING ***/

The above should work (again, untested -- sorry) as long as you don't
directly subvert it -- if you're worried about someone doing that, you
can put in another static assertion to guard against it, or structure
it a little differently by delegating to a helper based on the value of
is_pod.

A couple of notes on some pitfalls to avoid from the other (otherwise
perfectly helpful) suggestions you received:
* Never do a "using namespace std" declaration at a wider scope than an
individual function. It pollutes the global namespace and completly
defeats the purpose of the std namespace. Make use of using-directives
(e.g. "using std::string") instead, or explicitly qualify upon use.
* operator<<'s signature should be as I've given it -- it takes a
reference (non-const) to the stream as well as the object, and returns
the stream by reference. If you don't do it this way, you can't chain
them together. As such, it should not be a member function, either.
* Don't #include <iostream> if you're not using it.
* Make sure that you know the criteria is_pod uses, thoroughly. Check
the standard if you don't know the precise definition (it's not
entirely obvious), or the boost::is_pod documentation.

There are a lot of variants on how exactly to do the decision logic
within the bounds of partial template specialization. I heartily
recommend "Modern C++ Design" by Andrei Alexandrescu (waves @ Andrei --
I was a CS undergrad at UW during his grad stint there, I believe) and
"C++ Template Metaprogramming" by Abrahams and Gurtovoy.

Luke

Axter
Guest
 
Posts: n/a
#8: Dec 28 '05

re: Template for POD types only



Howard Hinnant wrote:[color=blue]
> In article <1135762499.665506.117510@g47g2000cwa.googlegroups .com>,
> "Raider" <sraider@yandex.ru> wrote:
>[color=green]
> > Is it possible to create a template for POD types only?
> >
> > I want to code something like this:
> >
> > class Object // base class
> > {
> > virtual void WriteToStream(Stream &) = 0;
> > };
> >
> > class Stream
> > {
> >
> > template <typename TPOD>
> > inline void operator << (TPOD &POD)
> > {
> > RawWrite(&POD, sizeof(TPOD));
> > }
> >
> > inline void operator << (Object &Obj)
> > {
> > Obj.WriteToStream(*this);
> > }
> >
> > };
> >
> > And I want compiler to use first method to write PODs and second to
> > write derived from Object (not Objects itself but derived from
> > Object!). Is it possible to tell compiler to use first method only for
> > PODs?[/color]
>
> Here's another approach (in addition to Axter's good suggestion). It
> also requires boost:
>
> #include <boost/utility/enable_if.hpp>
> #include <boost/type_traits.hpp>
>
> class Stream;
>
> class Object // base class
> {
> public:
> virtual void WriteToStream(Stream &) const = 0;
> };
>
> class Stream
> {
> public:
> template <typename TPOD>
> inline
> typename boost::enable_if<boost::is_pod<TPOD> >::type
> operator << (const TPOD &POD)
> {
> RawWrite(&POD, sizeof(TPOD));
> }
>
> inline void operator << (const Object &Obj)
> {
> Obj.WriteToStream(*this);
> }
>
> };
>
> This approach saves you the trouble of having to work through an
> implementation class (base_t in Axter's example). It more directly
> reflects your goal of restricting the template parameter TPOD to just
> pods, while allowing overloading to other operator<<.
>[/color]

**boost::enable_if**
Very nice trick.

Niklas Norrthon
Guest
 
Posts: n/a
#9: Dec 29 '05

re: Template for POD types only


"Axter" <google@axter.com> writes:
[color=blue]
> Here's some example code using boost is_pod:
>
> #include <iostream>
> #include <string>
>
> #include "boost\type_traits\is_pod.hpp"[/color]

Should be "boost/type_traits/is_pos.hpp"

(Yes on windows too!)

/Niklas Norrthon
Closed Thread