Topic: typeof' compile time functions?


Author: Max TenEyck Woodbury <mtew@cds.duke.edu>
Date: 1997/09/29
Raw View
Miles Sabin wrote:
>
> In <URL:news:comp.std.c++> on Fri 26 Sep, Max TenEyck Woodbury wrote:
> > { snip: motiviation that I'm not sure I completely understood ]
> >
> > Proposal:
> >
> >     'typeof' function_signature
> >     'typeof' (expr)
> >
> > A 'typeof' expression returns a type suitable for use in a
> > declaration, definition or typedef that is the type of the
> > specified expression or the return type of the
> > function_signature.
>
> I agree that a typeof operator would be useful. That said, I think
> that it's only absolutely essential in the case where we want to map
> an object onto a type.
>
> Your exmples are both of cases where we're trying to map a type onto
> a related type, and they can already be handled by the language as it
> stands using partial specialization (albeit nothing like as elegantly
> as would be possible with typeof).
>
> Example 1:
>
> > template <class ptr> class x {
> >     typedef ptr                     handle;
> >     typedef typeof(operator *(ptr)) base_type;
> >     ... };
>
> This can be dealt with as follows,
>
>   template<typename PtrMemFunc>
>   struct MemFunRetType
>   {};
>
>   template<class C, typename R, R (C::*ptrmemfun)()>
>   struct MemFunRetType<R (C::*ptrmemfun)()>
>   {
>     typedef R type;
>   };
>
>   template<typename ptr>
>   class x
>   {
>     typedef ptr handle;
>     typedef typename MemFunRetType<&ptr::operator*>::type base_type;
>
>     // etc.
>   };
>

I edited the above to make some of the names a little less cryptic.
I hope you don't mind.

The problem is with function signatures where there is more than one
parameter and where there are default parameters. I'll have to
think more about it but there might also be problems where the
function is overloaded or not a member function. In other words, it
has to work with the built in types as well as with added types.

    template<typename ptr> class y {
        typedef ptr handle;
        typedef typeof operator -( ptr, ptr) ptrdiff;
        ... };

> Example 2 :
>
> > template <class ptr> void y( ptr z) {
> >     typedef typeof(*z)              base_type;
> >     ... };
>
> Can be dealt with like this,
>
>   template<typename T>
>   struct DeRefType {};
>
>   template<typename T>
>   struct DeRefType<T*>
>   {
>     typedef T type;
>   };
>
>   template<typename ptr>
>   void y(ptr z)
>   {
>     typedef typename DeRefType<ptr>::type base_type;
>
>     // etc.
>   }
>

This isn't quite what I had in mind. It only does a _standard_
pointer dereference. 'ptr' might not be a real pointer; it
only has to act like a pointer and have 'operator *(ptr)' defined.
That's why it wasn't declared '(T * z)'.

The biggest problem I'm having building a work around for this
is that I don't want to evaluate the expression, just like the
expression in sizeof is not evaluated. On the other hand, the
expression may be arbitrarily complex. One of the uses for this
would be to hold the value of some common sub-expression that
appears several places in a function and is too complex (for at
least some of compiler that the function is to be built on) to
factor out.

> Usual disclaimers apply in buckets: I don't have access to a compiler
> anything like up to date enough to check the above out.
>

And since the draft isn't approved yet, even the most up to date
compilers may have to change...

mtew@cds.duke.edu
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Max TenEyck Woodbury <mtew@cds.duke.edu>
Date: 1997/09/26
Raw View
I have been writing some template classes and noticed a problem:

Given a type template parameters, it is only possible to specify
the type, cv qualifications of the type and various standard
derivations of the type. If you want to allow for non-standard
derivations, you have to pass in the 'derived' types as template
parameters. This opens the door to quite a number of errors. In
particular, it is possible to become confused about which
parameter is the derived type of the other.

Before I go on, I note that it is too late to make the following
kind of change to this version of the standard. The proposal is
for a 'standard extension' that will be considered for the next
round.

Proposal:

    'typeof' function_signature
    'typeof' (expr)

A 'typeof' expression returns a type suitable for use in a
declaration, definition or typedef that is the type of the
specified expression or the return type of the
function_signature.

example:

template <class ptr> class x {
    typedef ptr                     handle;
    typedef typeof(operator *(ptr)) base_type;
    ... };

template <class ptr> void y( ptr z) {
    typedef typeof(*z)              base_type;
    ... };

I'll leave the exact tyntax and semantics to someone more expert
than I am to nail down, but I think this conveys the idea well
enough.

mtew@cds.duke.edu
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Miles Sabin <miles@mistral.co.uk>
Date: 1997/09/27
Raw View
In <URL:news:comp.std.c++> on Fri 26 Sep, Max TenEyck Woodbury wrote:
> { snip: motiviation that I'm not sure I completely understood ]
>
> Proposal:
>
>     'typeof' function_signature
>     'typeof' (expr)
>
> A 'typeof' expression returns a type suitable for use in a
> declaration, definition or typedef that is the type of the
> specified expression or the return type of the
> function_signature.

I agree that a typeof operator would be useful. That said, I think
that it's only absolutely essential in the case where we want to map
an object onto a type.

Your exmples are both of cases where we're trying to map a type onto
a related type, and they can already be handled by the language as it
stands using partial specialization (albeit nothing like as elegantly
as would be possible with typeof).

Example 1:

> template <class ptr> class x {
>     typedef ptr                     handle;
>     typedef typeof(operator *(ptr)) base_type;
>     ... };

This can be dealt with as follows,

  template<typename PMFn>
  struct MFnReturnType
  {};

  template<class C, typename R, R (C::*pmfn)()>
  struct MFnReturnType<R (C::*pmfn)()>
  {
    typedef R type;
  };

  template<typename ptr>
  class x
  {
    typedef ptr handle;
    typedef typename MFnReturnType<&ptr::operator*>::type base_type;

    // etc.
  };

Example 2 :

> template <class ptr> void y( ptr z) {
>     typedef typeof(*z)              base_type;
>     ... };

Can be dealt with like this,

  template<typename T>
  struct DeRefType {};

  template<typename T>
  struct DeRefType<T*>
  {
    typedef T type;
  };

  template<typename ptr>
  void y(ptz z)
  {
    typedef typename DeRefType<ptr>::type base_type;

    // etc.
  }

Usual disclaimers apply in buckets: I don't have access to a compiler
anything like up to date enough to check the above out.

Cheers,


Miles

--
Miles Sabin                             mailto:miles@mistral.co.uk
Cathexis                                voice  +44 (0)1273 732338
                                        letter 10 Pembroke Gardens
                                               Brighton, BN3 5DY, UK
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]