Boost logo

Boost :

From: Herve Bronnimann (hbr_at_[hidden])
Date: 2002-06-21 08:59:56


On Fri, Jun 21, 2002 at 07:27:17AM -0400, David Abrahams wrote:
> Joel de Guzman wrote:
> > identity is a nice name. However, the name itself does not imply
> > that it is a metafunction. I know it would be longish but I feel
> > that type_identity would be better (it's just 1 char longer than
> > type_wrapper).

I was going to make the same suggestion! If the purpose of identity is
top be a metafunction, or a function on types, then type_ is the natural
prefix.

> Or we could make it an identity for all seasons?
>
> template <class T = mpl::not_specified>
> struct identity : std::unary_function<T,T>
> {
> typedef T type;
> T operator()(T x) const { return x; }
> };
>
> template<>
> struct identity<mpl::not_specified>
> {
> template <class T>
> struct apply { typedef T type; };
> }
>
> I'm sort of kidding... sort of.

Well, if you go that way, there is an identity_property_map in
<boost/property_map.hpp> (although I've already pointed out to Jeremy
that it should really be a class template to have the type). Granted, it
uses operator[] instead of opertor(), so it seems to be somewhat
distinct.

For people who've seen that in the standard, I'd suggest using
"rebind" instead of "apply".

I would favor giving just a single class (not two) and as a result
getting rid of the difference in interface between the class template
and its specialization (which could be evil in some cases):

   template <class T>
   struct identity
     : std::unary_function<T,T> // defines argument_type, result_type
   {
       typedef T type; // somewhat redundant with unary_function,
                       // but apparently to satisfy mpl's interface

       template <class U> struct rebind { typedef identity<U> other; };

       T operator()(T x) const { return x; }
   };

Best,

-- 
Hervé

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