Topic: operators with (additional) template args


Author: Axel Facius <axel.facius@math.uni-karlsruhe.de>
Date: 1999/10/14
Raw View
Hi,

I intend to write a C++ library that allows arithmetic
operations with directed rounding.
A first (ugly) try was to write functions like

-------------------------------------------------------
class IEEEfloat { ... };

IEEEfloat add_and_round_up( IEEEfloat, IEEEfloat );
-------------------------------------------------------

but I really like that infix notation, so I tried
something like

-------------------------------------------------------
template<class RND> IEEEfloat operator+( IEEEfloat );
class UP { ... };

IEEEfloat x, y, z;
x = y +<UP> z;             // (*)
-------------------------------------------------------

Unfortunately I found no compiler that understands me.
The best I got was Kuck & Associates KCC (3.4d) which
at least compiled

-------------------------------------------------------
x = y.operator+<UP> z;
-------------------------------------------------------

Could some std guy/langlawyer clarify for me if (*) is
legal C++ code or are there any counter examples why an
expression like this is not unique.

Thanks in advance.

 Axel.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "TiTi" <TJunkMyAss@mad.scientist.com>
Date: 1999/10/14
Raw View
[Original post can be found at the end of this post]

Following compiles under VC++6, without errors

class UP {};
class IEF {};
template <class RND> IEF opf(IEF& x, IEF& y)
{ return x=y; };
int main(void)
{
 IEF x, y, z;
 x = opf <UP> (y, z);
 return 0;
};


I tested the same with operator +, and it didn't compile (error C2275: 'UP'
: illegal use of this type as an expression):

class UP {};
class IEF {};
template <class RND> IEF operator+(IEF& x, IEF& y)
{ return x=y; };
int main(void)
{
 IEF x, y, z;
 x = operator + <UP> (y, z);
 return 0;
};

Didn't check the standard, but it seems OK qua syntax. Sorry, can't tell ya
for sure. But as it doesn't compile (under at least one compiler), it's
likely to be non-standard. Do mind I can't tell you for sure. Only tested
one compiler.

Maybe you should try putting this kind of operations in the IEF (short for
IEEEfloat) class. No need to write a seperate template function then. Just
guessing here.


TiTi




> I intend to write a C++ library that allows arithmetic
> operations with directed rounding.
> A first (ugly) try was to write functions like
> template<class RND> IEEEfloat operator+( IEEEfloat );
> class UP { ... };
>
> IEEEfloat x, y, z;
> x = y +<UP> z;             // (*)
> -------------------------------------------------------
> Unfortunately I found no compiler that understands me.
> The best I got was Kuck & Associates KCC (3.4d) which
> at least compiled
> -------------------------------------------------------
> x = y.operator+<UP> z;
> -------------------------------------------------------
> Could some std guy/langlawyer clarify for me if (*) is
> legal C++ code or are there any counter examples why an
> expression like this is not unique.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]