Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-01-06 11:41:53


Yitzhak Sapir <yitzhaks_at_[hidden]> writes:

> I think storing the text "Hello world!" in a vector
>
> typedef vector13_c<char,
> 'H','e','l','l','o',' ',
> 'w','o','r','l','d','!','\n'> hello_world;
>
> And then using functors to print it such as:
>
> template <class Prev, class T> struct print
> {
> static void eval()
> {
> Prev::eval();
> std::cout.put(T::value);
> }
> };
>
> fold<vector,...>::type::eval();
>
> would work as an hello world. It introduces two main concepts (fold,
> vector), shows how MPL can be used for loop unrolling (on proper
> compilers, the entire Hello world text would be unrolled into
> successive calls cout.put), and makes sense for most people as an "Hello
> world".

That's a very interesting idea!
I think I'd use mpl::for_each instead of fold<> here:

typedef vector13_c<char,
    'H','e','l','l','o',' ',
    'w','o','r','l','d','!','\n'> message;

struct printer
{
   template <class T>
   void operator()(T&) const
   {
        std::cout.put(T::value);
   }
};

for_each<message,identity>(printer);

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk