Topic: typeof vs. auto (was C++0x)


Author: Olaf Krzikalla <Entwicklung@reico.de>
Date: Tue, 8 May 2001 23:28:48 GMT
Raw View
Hi,

To make it clear: I'm not against typeof. I believe, it's power is
already proven in a lot of gcc projects. But I'm not convinced yet.

Joseph Gottman wrote:
>    This is simpler, but typeof() is more flexible.  For instance, suppose
> you want to write a function that adds two polynomials of different types.
> You could declare it as
>
> template <class X, class Y>
> polynomial<typeof(X() + Y())> operator+(
>      const polynomial<X> &f,
>      const  polynomial<Y> &g);
>
> I don't think auto as you use it above would help in this case.

Granted, for function declaration typeof seems to be needed. But:
1. I don't like the syntax. It doesn't look like a type to me. Also it
looks like X and Y need default ctors.
2. When X and Y become certain classes (say A and B), you need the
declaration of
A+B even if you don't use it. For your example this might be ok, but I
can think of cases, where you get a lot of unwanted dependencies.

How would we solve the shown example today? I would use traits. This has
also some disadvantages:
1. The interface becomes larger, because classes for X and/or Y now
often needs a specialized trait in addition to the operator+.
2. It's often not simple to add new properties to traits.

Templated typedef's could solve the second problem:

template<class X, class Y> typedef X addType;

// some specialisation:
template<class X> typedef double addType<X, double>; // could be made in
the STL

template<class A, class B> typedef
  BinExpr<Expr<A>, Expr<B>, ApplyAdd> addType<Expr<A>, Expr<B> >;

template<class A, class B>
addType<Expr<A>, Expr<B> > operator+ (Expr<A>&, Expr<B>&);

template <class X, class Y>
polynomial< addType<X, Y> > operator+(
  const polynomial<X> &f,
  const polynomial<Y> &g);

Note, that you have to write the type for the user-defined operator+
somewhere - either in the declaration of the operator+ or in a templated
typedef (and then use only the shortcut for the operator+ declaration).


Best regards
Olaf Krzikalla

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Olaf Krzikalla <Entwicklung@reico.de>
Date: Tue, 8 May 2001 23:28:55 GMT
Raw View
Hi,

To make it clear: I'm not against typeof. I believe, it's power is
already proven in a lot of gcc projects. But I'm not convinced yet.

Joseph Gottman wrote:
>    This is simpler, but typeof() is more flexible.  For instance, suppose
> you want to write a function that adds two polynomials of different types.
> You could declare it as
>
> template <class X, class Y>
> polynomial<typeof(X() + Y())> operator+(
>      const polynomial<X> &f,
>      const  polynomial<Y> &g);
>
> I don't think auto as you use it above would help in this case.

Granted, for function declaration typeof seems to be needed. But:
1. I don't like the syntax. It doesn't look like a type to me. Also it
looks like X and Y need default ctors.
2. When X and Y become certain classes (say A and B), you need the
declaration of
A+B even if you don't use it. For your example this might be ok, but I
can think of cases, where you get a lot of unwanted dependencies.

How would we solve the shown example today? I would use traits. This has
also some disadvantages:
1. The interface becomes larger, because classes for X and/or Y now
often needs a specialized trait in addition to the operator+.
2. It's often not simple to add new properties to a already existing
trait.

Templated typedef's could solve the second problem:

template<class X, class Y> typedef X addType;

// some specialisation:
template<class X> typedef double addType<X, double>;    // could be made
in
the STL

template<class A, class B> typedef
  BinExpr<Expr<A>, Expr<B>, ApplyAdd> addType<Expr<A>, Expr<B> >;

template<class A, class B>
addType<Expr<A>, Expr<B> > operator+ (Expr<A>&, Expr<B>&);

template <class X, class Y>
polynomial< addType<X, Y> > operator+(
  const polynomial<X> &f,
  const polynomial<Y> &g);

Note, that you have to write the type for the user-defined operator+
somewhere - either in the declaration of the operator+ or in a templated
typedef (and then use only the shortcut for the operator+ declaration).


Best regards
Olaf Krzikalla

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]