Boost logo

Boost :

From: James S. Adelman (j.adelman_at_[hidden])
Date: 2001-11-28 17:25:43


On Wednesday 28 November 2001 7:52 pm, you wrote:
> On Wednesday, November 28, 2001, at 12:32 PM, James S. Adelman wrote:
> > Nonetheless, two symbols are equal only if one can be used in place of
> > another. This is clearly not the case for int2type, since it maps an
> > int to
> > a type, and it is not the case for type2type, since the types are
> > distinct.
>
> wrt int2type, agreed.
>
> You're still not looking at type2type the way I am. Adrei's type2type is
> defined thus:
>
>
> template <typename T>
> struct Type2Type
> {
> typedef T OriginalType;
> };
>
> If you regard Type2Type as a metafunction, then T is its parameter and
> OriginalType is its return value, in which case it is a true identity
> function.
>
> I realize that that isn't the purpose for which these templates are
> defined in Andrei's code. They really are wrappers, in general used to
> prevent some properties of their parameters from coming into play (e.g.
> a reference type or a compile-time constant integer can be wrapped into
> a value type), so that types and objects can be treated with more
> uniformity.

To be clear, I would say that typename Type2Type<X>::OriginalType is an
identity function with the argument X, but there is no more succinct way to
alias this, and Type2Type<X> is not an identity function in any form. This
is the same reasoning that says that the 1x1 matrix (1) is different from the
number 1. However, in that case there is no conflict between the meaning of
the matrix and the number, except that a broader range of operations can be
applied to the number. Alternatively consider:

template<class X> struct A {X x;};
template<class X> A<X> identity(X x){A<X> a; a.x=x; return a;}
  // is this what you expect from an identity function?
  // This is what the template Type2Type does.

Returning to the main point:

(i) X x;
(ii) Type2Type<X> x;
(iii) typename Type2Type<X>::OriginalType x;

The first and third are identical to each other, and differ from the second.
There is no sense in which Type2Type<X> stands in for X. Since the value of
identity(x) with x a value is x, the type of identity<X> with X a type should
be X. Since it cannot be, that expectation shouldn't be given.


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