Topic: To 'typeof' or not to 'typeof'?
Author: "Joe Gottman" <joegottman@worldnet.att.net>
Date: Sat, 9 Mar 2002 16:31:04 GMT Raw View
"Daniel Frey" <daniel.frey@aixigo.de> wrote in message
news:3C8876E2.803CA800@aixigo.de...
Hello, world!
while reading a message over at the boost mailing list, I was wondering
if the following idea could work and if it is a good idea:
Consider 'typeof' and one of its proposed uses:
template< typename lhs_t, typename rhs_t >
struct binary_operator_traits {
typedef typeof( lhs_t() + rhs_t() ) arithmetic;
};
While this looks good at the first sight, it has an obvious drawback: It
requires the default-ctor. A solution was proposed at boost:
template< typename T > T make();
template< typename lhs_t, typename rhs_t >
struct binary_operator_traits {
typedef typeof( make< lhs_t >() + make< rhs_t >() ) arithmetic;
};
Now I wonder if the whole typeof-thing couldn't be approached by a
different angle. Currently, you cannot apply operators to types. But
obviously, this is what the guys that wrote the above code intended. Now
look at the following code:
X x;
Y y;
x + y;
Seeing 'x + y', the compiler knows the types of x and y, X and Y. Now it
has to find an operator+ that works on X and Y. This is independent of
the actual instances x and y, it is only based on the types. The rules
are already defined. If the compiler knows the operator to use, it also
knows the type of the expression 'x + y'. The proposed language
extension is now a very simple one: Allow using operators on types. The
above example would look like this:
template< typename lhs_t, typename rhs_t >
struct binary_operator_traits {
typedef lhs_t + rhs_t arithmetic;
};
AFAIKS no existing code would be broken and all rules on how the
resulting type should be defined are already there. It would IMHO be a
very consistent extension to the rest of the language. The benefits are
clearer and simpler code, probably most useful in
template-metaprogramming. The above 'hacks' like 'make< lhs_t >()' would
be avoided... hm... anything I forgot? Well, I know that this will not
replace 'typeof' in all cases, but at least some cases would be easier.
Why not just say that within typeof() lhs_type() is legal for any
typename lhs_type, whether or not it has a default constructor? Then the
original construct will be correct in all cases.
Joe Gottman
---
[ 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: "Ludwig Pumberger" <pumberger@direkt.at>
Date: Sun, 10 Mar 2002 16:04:20 GMT Raw View
"Daniel Frey" <daniel.frey@aixigo.de> wrote in message:
>Allow using operators on types. The above example would look like this:
>template< typename lhs_t, typename rhs_t >
>struct binary_operator_traits {
> typedef lhs_t + rhs_t arithmetic;
>};
There is already an operator working on typenames: The operator ().
How would you interpret this(T is a typename):
T()
Default construct of T is invoked, or
the result type of T::operator() ?
---
[ 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: Daniel Frey <daniel.frey@aixigo.de>
Date: Mon, 11 Mar 2002 17:51:23 GMT Raw View
Joe Gottman wrote:
>=20
> "Daniel Frey" <daniel.frey@aixigo.de> wrote in message
> news:3C8876E2.803CA800@aixigo.de...
>
> > template< typename T > T make();
> > template< typename lhs_t, typename rhs_t >
> > struct binary_operator_traits {
> > typedef typeof( make< lhs_t >() + make< rhs_t >() ) arithmetic;
> > };
>=20
> Why not just say that within typeof() lhs_type() is legal for any
> typename lhs_type, whether or not it has a default constructor? Then t=
he
> original construct will be correct in all cases.
I don't think it is a good idea to add a special case here, the above
make<T>()-work-around is good enough. Your solution would lead to code
that says T() but doesn't do so, which is IMHO not intuitive. I tried to
find a general meaning for T+U, but I probably failed, see the other
post :)
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de
---
[ 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: Daniel Frey <daniel.frey@aixigo.de>
Date: Mon, 11 Mar 2002 11:59:04 CST Raw View
Ludwig Pumberger wrote:
>
> "Daniel Frey" <daniel.frey@aixigo.de> wrote in message:
>
> >Allow using operators on types. The above example would look like this:
>
> >template< typename lhs_t, typename rhs_t >
> >struct binary_operator_traits {
> > typedef lhs_t + rhs_t arithmetic;
> >};
>
> There is already an operator working on typenames: The operator ().
> How would you interpret this(T is a typename):
>
> T()
>
> Default construct of T is invoked, or
> the result type of T::operator() ?
Indeed. A good point to think of when designing a new language ;) And I
found another one:
...
T1 tmp;
{ // New scope
T2<T3> tmp;
}
...
If parsed as operator<(T2,T3) > t, the resulting errors may be very
entertaining for the user :) Probably not my best idea... except someone
finds a good, clean and easy solution to solve all the problems... but
that's not me. Not today :(
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schlo -Rahe-Stra e 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de
---
[ 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: Daniel Frey <daniel.frey@aixigo.de>
Date: Fri, 8 Mar 2002 17:25:52 GMT Raw View
Hello, world!
while reading a message over at the boost mailing list, I was wondering
if the following idea could work and if it is a good idea:
Consider 'typeof' and one of its proposed uses:
template< typename lhs_t, typename rhs_t >
struct binary_operator_traits {
typedef typeof( lhs_t() + rhs_t() ) arithmetic;
};
While this looks good at the first sight, it has an obvious drawback: It
requires the default-ctor. A solution was proposed at boost:
template< typename T > T make();
template< typename lhs_t, typename rhs_t >
struct binary_operator_traits {
typedef typeof( make< lhs_t >() + make< rhs_t >() ) arithmetic;
};
Now I wonder if the whole typeof-thing couldn't be approached by a
different angle. Currently, you cannot apply operators to types. But
obviously, this is what the guys that wrote the above code intended. Now
look at the following code:
X x;
Y y;
x + y;
Seeing 'x + y', the compiler knows the types of x and y, X and Y. Now it
has to find an operator+ that works on X and Y. This is independent of
the actual instances x and y, it is only based on the types. The rules
are already defined. If the compiler knows the operator to use, it also
knows the type of the expression 'x + y'. The proposed language
extension is now a very simple one: Allow using operators on types. The
above example would look like this:
template< typename lhs_t, typename rhs_t >
struct binary_operator_traits {
typedef lhs_t + rhs_t arithmetic;
};
AFAIKS no existing code would be broken and all rules on how the
resulting type should be defined are already there. It would IMHO be a
very consistent extension to the rest of the language. The benefits are
clearer and simpler code, probably most useful in
template-metaprogramming. The above 'hacks' like 'make< lhs_t >()' would
be avoided... hm... anything I forgot? Well, I know that this will not
replace 'typeof' in all cases, but at least some cases would be easier.
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de
---
[ 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 ]