|
Boost : |
From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2002-06-26 15:19:51
>From: "Ralf W. Grosse-Kunstleve" <rwgk_at_[hidden]>
> --- Samuel Krempp <krempp_at_[hidden]> wrote:
> > On Thu, 2002-06-20 at 21:14, David Abrahams wrote:
> >
> > > suggested identity<> before; nobody seemed to get the whole
metafunction
> > > idea, so for the record, I don't care all that much what we call it.
> >
> > If I think of a "identity" metafunction, I would expect it to follow the
> > properties of a regular identity :
> > "x" is perfectly equivalent to "identity(x)"
> >
> > But here "T" is equivalent to "identity<T>::type",
> > not just "identity<T>".
> >
> > For me, it's not identity.. it's a bijective metafunction all-right, but
> > not identity.
> > I thought injective metafunctions were usually called "wrappers" -and in
> > fact those wrappers often are as much bijective as this identity<T>
> > template. The difference is just that this one is intended to serve as
> > as an identity metafunction. (since it's the closest we can get to such
> > a thing. there is no "let f(T) = T" possible here..)
> >
> > So, personnally, I'd rather choose for a weaker, but correct, name based
> > on 'wrapper',
> > than a more precise, but slightly incorrect name based on 'identity'..
> >
> > ah, I thought of a clear manifestation of the difference between a true
> > identity and this metafunction.
> > Mathematically, identity composed with itself is still identity.
> > So I'd expect
> > identity< identity<T> > to be the exact same thing as
> > identity<T>
> > And, unless you add a specialisation, it's not the case.
> >
> > Still, the
> > T operator()(T x) const { return x; }
> > really is an identity.
> > So if it's included in the struct, it could be named identity<T>, due to
> > this function (rather than to the struct viewed as metafunction, which
> > is *not* an identity).
>
> I find Samuel's arguments very convincing. To add another thought: once
there
> are template typedef's you might want to use "identity" there. I am not
> familiar with the proposed syntax, but let me guess:
>
> template <typename T> typedef identity T;
>
> But even here I'd prefer "identity_type" or "type_identity" .
>
> To me, "type_wrapper<>" seems to be the most appropriate name for what is
> currently called "type<>" .
I guess this depends on point of view.
As has been pointed out, identity<T> is not the same as T, but
identity<T>::type is. However, in run-time programming, identity(x) _is_ the
same as x. The thing is that, as we know, any types or values to be
"returned" from templates, has to be done using named return values, such as
typedefs or static constants. Thus, the compile-time identity<T>::type is
rather like identity(x), except that the last one uses an unnamed return
value, while the first names it, "type".
What may cause confusion, is that in the case of identity<T> and
identity<T>::type, both are types. The first is a unique type, while the
last gives the type that was used to construct the unique type. This has
been called different things. Loki has Type2Type<T>, which I think is quite
succint: It creates a new type, based on another type - the argument. As a
"bonus", the type used to create it, is stored as an inner typedef. There's
also an Int2Type<n>, with similar functionality, creating a type, based on
an integer template parameter, and stores it as a static constant in the
type.
As is mentioned here, type_wrapper<T> has also been suggested.
When I first read about identity<T>, I didn't really understand it, because
surely, identity<T> and T could not be the same. Then, I realized the
metafunction thought behind it, that it's a metafunction, returning T as an
inner type.
Regarding what Samuel says, here, identity<identity<T> > is not T, but
identity<identity<T>::type>::type is. So it's a matter of understanding that
it's a metafunction.
Regards,
Terje
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk