Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ trait in class

0 views
Skip to first unread message

Berend de Boer

unread,
Dec 13, 2002, 4:32:10 AM12/13/02
to
Jean-Christophe Taveau <jc.t...@iecb-polytechnique.u-bordeaux.fr> writes:

> A trait class (Myers, 1995; http://www.cantrip.org/traits.html) is a
> very useful technique to parameterize numeric types in a C++ template
> (generic class). For example, this is used in the C++ generic image
> library vigra
> (http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/).
>
>
> I tried to use them in Eiffel but I was unsuccessfull. Perhaps there
> is a workaround that I don't know.

I've never heard of this technique, could you show what you think it
would be in Eiffel?

I've sent a reply to comp.lang.eiffel as well, because I believe it's
more a general Eiffel than SmartEiffel specific technique, that also
means we're able to attract attention of other Eiffel useres.

--
Live long and prosper,

Berend de Boer

Leopold Faschalek

unread,
Dec 13, 2002, 4:43:32 AM12/13/02
to
"Berend de Boer" <ber...@pobox.com> wrote in message
news:3DF9A91A...@pobox.com...

I think they are used to implement a kind of constrained genericity
in C++ similar to eiffel.
have a look at http://thad.notagoth.org/cpptraits_intro/

> --
> Live long and prosper,
>
> Berend de Boer
>

greetings Leopold Faschalek


P.Poncin

unread,
Dec 13, 2002, 6:08:25 AM12/13/02
to
Two ideas:

-Another interesting feature of C++ templates is that they provide means to
parametrize single methods (if I remember this correctly).

-Also parametizing classes not on the type level but on the value level
could be useful.
Consider e.g.:

class TEXT_FILE[NEWLINE]
feature
newline_sequences : STRING is NEWLINE
end

class UNIX_TEXT_FILE["%/10/"]
end

class MAC_TEXT_FILE["%/10/%/13/"]
end


(Berend, sorry if you got this as a mail, too. My newsreader did not do what
I wanted)


Tim Walkenhorst

unread,
Dec 14, 2002, 12:13:53 PM12/14/02
to
> I've never heard of this technique, could you show what you think it
> would be in Eiffel?

Since I'm not an Eiffel Expert I can't come up with a way to implement traits in
Eiffel, or an answer whether traits are needed in Eiffel or not, but I hope I
can give you a glimpse on the applications for traits.

Traits represent a structure of compile time informations. Like you can store
different attributes in a class you can group different compile time attributes
(which are types(which can themthelves be traits) or integral constants) in a
traits class. Technically you need a typedef and an enumeration feature in order
to implement traits.

Consider the following example (in C++ I think there is no aequivalent in Eiffel):

struct nil; // <- Empty List

template <class h, class t>
struct list {
typedef h head;
typedef t tail;
};

// compile time list: int->double->string->nil
typedef list<int, list < double, list <string, nil> > > intDoubleString;

// compile time calculation of size
template <class l>
struct size {
enum {result=1+size<typename l::tail>::result}; // <- Recursion
};

template <>
struct size <nil> {
enum {result=0}; // <- Starting point for recursion
};

// listSize (3) is computet at compile time
const int listSize=size<intDoubleString>::result;

I guess this was a little confusing for anyone not familiar with c++, but I
hope you got a clue for what traits are used. Of course this technique can be
used to implement constrained genericity in c++, but the range of applications
is much wider. Technically you have a turing complete compile time language
within c++ based on the following language features: typedef and enums(compile
time variables), template specialization (compile time "if") and recursive
template use (compile time loop).

In order to get more information you can try the following keywords:
-Template Metaprogramming
-Generative Programming
-Andrei Alexandrescu
-Ulrich Eisenecker
-Krzysztof Czarnecki

Since template metaprogramming made it into c++ by accident and not by
design usability is quiet low. It would be interesting to hear if the Eiffel
community has ever thought about a way to enable compile time programming in
Eiffel, or if compile time programming is considered useful at all.

Tim

P.Poncin

unread,
Dec 20, 2002, 5:59:49 AM12/20/02
to
Sorry, I meant to write:

class TEXT_FILE[NEWLINE]
feature
newline_sequences : STRING is NEWLINE
end

class UNIX_TEXT_FILE
inherit
TEXT_FILE["%/10/"]
end

class MAC_TEXT_FILE
inherit
TEXT_FILE["%/10/%/13/"]
end


Mike Meyer

unread,
Dec 20, 2002, 6:05:00 AM12/20/02
to
"P.Poncin" <Pascal...@T-Online.de> writes:

How about this:

deferred class TEXT_FILE
feature newline_sequence: STRING is deferred
-- rest of class elided
end

class UNIX_TEXT_FILE
inherit TEXT_FILE
feature newline_sequence: STRING is "%/10/"
end

class MAC_TEXT_FILE
inherit TEXT_FILE
feature newline_sequence: STRING is "%/10/%/13/"
end

<mike
--
Mike Meyer <m...@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

0 new messages