Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 1999-07-08 20:39:33


Beman Dawes wrote:
>
> At 05:05 PM 7/7/99 +0200, Valentin Bonnard wrote:
>
> >// very useful example of the use of traits
>
> Valentin, you lost me.

Sorry.

> Did you mean for the definition of foo to be
> preceded by template<class T>? And isn't bar(Red()); etc, supposed
> to be foo(Red()); etc?
> I guess #include "fobar.hpp" is supposed to be
> #include "foobar.hpp"

yes, yes, and yes

> Plus you better include the source for
> sometrait.hpp. Sorry to be old-fashioned, but I would actually like
> to compile and run your example to make sure I understand it.

OK (I really wrote the code fastly and it wasn't meant to compile)

With egcs, the following compiles and run:

// very useful example of the use of traits

// "sometrait.hpp"
struct yes {};
struct no {};
struct maybe {};

template <typename T>
struct sometrait {
    typedef maybe is_red;
};

// foobar.hpp
// #include "sometrait.hpp"

#include <iostream>

using std::cout;

void bar (maybe)
{
    cout << "color maybe\n";
}

void bar (yes)
{
    cout << "red\n";
}

void bar (no)
{
    cout << "non red\n";
}

template <typename T>
void foo (T)
{
    bar (sometrait<T>::is_red ());
}

// #include "fobar.hpp"

struct Red {};
struct Alien {};

template <>
struct sometrait<Red> {
    typedef yes is_red;
};

int main ()
{
    foo (Red ());
    foo (Alien ());
}

prints

red
color maybe

-- 
Valentin Bonnard
------------------------------------------------------------------------
eGroups.com home: http://www.egroups.com/group/boost
http://www.egroups.com - Simplifying group communications

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