• entries
146
436
• views
197363

# So...

233 views

Would you like to hear about C++ Type traits in std::tr1, and how to use them for compiletime decisions?

a friend rolled his own for his event system. there seems to be alot of uses for traits, especially when passing parameters around (the classic "pass a copy for PODs and const refs for the rest"). I'd sure like to hear more, especially the one proposed to C++ :D

Well, yes...

...no...

...maybe...

...only if you talk about runtime implementation...

...I have no idea what you're talking about.

-IV

Ooooo, compile time decision making sounds SEXY! [grin]

i vote yes! [grin]

Washu, you know we're going to read and enjoy anything you write. Plus, it'd be great to read some of your insights on C++ for a change (as opposed to the usual .NET tidbits). In short:

YES

I use type traits quite abit in my code but less on its own more with boost MPL. I have some simple code that does what Seriema was describing about passing parameters by value or constant reference:

#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/type_traits/is_fundamental.hpp>

template < typename Tq >
struct param_type : public boost::mpl::eval_if<
boost::is_fundamental<Tq>,
boost::mpl::identity<Tq>,
> {};


usage:

template < typename Tp >
struct foo {   void bar(typename param_type<Tp>::type) {}   };


its nifty [grin].

There is all kind of things you can do even make domain specific embedded languages, if you get the book C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond you'll see all the ways in which you can use them.

Yes.

Anything other than .NET would be appreciated.

## Create an account

Register a new account