Bug in boost::rational, no Boolean conversion; an operator template suggestion

I'm writing a class template where the template parameter is a type that should represent real numbers. It started choking when using boost::rational when I needed Boolean conversion, e.g. template < typename T > void my_class<T>::my_func() { if ( this->s_ ) //... } Looking at boost::rational, I noticed that it does NOT have a Boolean conversion operator (i.e. an unspecified numeric/pointer type). Before anyone says that's proper, realize that boost::rational DOES have "operator not()" defined! This is a surprise to the typical C++ programmer. Either both should be defined, or neither. I think that boost::rational should add a Boolean conversion. (It's a quick fix, and it is critical, so it should be done for this release.) Incidentally, maybe we should define another helper template for <boost/operators.hpp>. If a type supports default construction and equality via "operator ==()", then a Boolean conversion and "operator not()" will be added. (It's up to the user to make sure the type is numeric and the default value is equivalent to zero.) -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com

Daryle Walker wrote:
I'm writing a class template where the template parameter is a type that should represent real numbers. It started choking when using boost::rational when I needed Boolean conversion, e.g.
Before anyone says that's proper, realize that boost::rational DOES have "operator not()" defined! This is a surprise to the typical C++ programmer. Either both should be defined, or neither. I think that boost::rational should add a Boolean conversion. (It's a quick fix, and it is critical, so it should be done for this release.)
Off the top of my head, I can't think why it shouldn't be added. However, there might be a good reason I can't think of, and adding the operator could conceivably break existing code, so I don't think it's appropriate to stick it in a few days before release. I'll add it to the list of feature requests for 1.34. In the mean time, maybe you can use != 0 or !! Jonathan

On 7/17/05 1:57 AM, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
Daryle Walker wrote:
I'm writing a class template where the template parameter is a type that should represent real numbers. It started choking when using boost::rational when I needed Boolean conversion, [...]
Before anyone says that's proper, realize that boost::rational DOES have "operator not()" defined! This is a surprise to the typical C++ programmer. Either both should be defined, or neither. I think that boost::rational should add a Boolean conversion. (It's a quick fix, and it is critical, so it should be done for this release.)
Off the top of my head, I can't think why it shouldn't be added. However, there might be a good reason I can't think of, and adding the operator could conceivably break existing code, so I don't think it's appropriate to stick it in a few days before release.
Boolean conversions are currently banned, so they couldn't be in existing code. The worry is expressions where a conversion would be higher priority that whatever operation is currently used. The only operations I can think of in that category are other conversions, but a new Boolean conversion can't cause problems because C++ allows at most one user-conversion (i.e. the new operation is of lower priority). If we use a member-pointer and keep the type & values used private, then no one can name the type to grab it directly. The class template already defines equality operators, so we don't have to worry about the member-pointer conversion adding inappropriate operators, especially if we make each version of boost::rational maintain separate Boolean types.
I'll add it to the list of feature requests for 1.34.
I put it in our SourceForge bug tracker as Request ID #1239906, so we won't forget.
In the mean time, maybe you can use != 0 or !!
I have "x != T()" right now, but I want to keep the code clean. Especially since I specified Boolean conversion as a requirement. (I didn't know about boost::rational's deficiency at first.) -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com

Daryle Walker wrote:
On 7/17/05 1:57 AM, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
Daryle Walker wrote:
I'm writing a class template where the template parameter is a type that should represent real numbers. It started choking when using boost::rational when I needed Boolean conversion, [...]
Before anyone says that's proper, realize that boost::rational DOES have "operator not()" defined! This is a surprise to the typical C++ programmer. Either both should be defined, or neither. I think that boost::rational should add a Boolean conversion. (It's a quick fix, and it is critical, so it should be done for this release.)
Off the top of my head, I can't think why it shouldn't be added. However, there might be a good reason I can't think of, and adding the operator could conceivably break existing code, so I don't think it's appropriate to stick it in a few days before release.
Boolean conversions are currently banned, so they couldn't be in existing code. The worry is expressions where a conversion would be higher priority that whatever operation is currently used. The only operations I can think of in that category are other conversions, but a new Boolean conversion can't cause problems because C++ allows at most one user-conversion (i.e. the new operation is of lower priority). If we use a member-pointer and keep the type & values used private, then no one can name the type to grab it directly. The class template already defines equality operators, so we don't have to worry about the member-pointer conversion adding inappropriate operators, especially if we make each version of boost::rational maintain separate Boolean types.
As I said, I can't think why it shouldn't be added.
I'll add it to the list of feature requests for 1.34.
I put it in our SourceForge bug tracker as Request ID #1239906, so we won't forget.
Okay.
In the mean time, maybe you can use != 0 or !!
I have "x != T()" right now, but I want to keep the code clean. Especially since I specified Boolean conversion as a requirement. (I didn't know about boost::rational's deficiency at first.)
Jonathan

On 7/17/05 5:34 PM, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
Daryle Walker wrote:
On 7/17/05 1:57 AM, "Jonathan Turkanis" <technews@kangaroologic.com> wrote: [SNIP note that boost::rational supports "operator !" but not Boolean conversion] As I said, I can't think why it shouldn't be added.
I'll add it to the list of feature requests for 1.34.
I put it in our SourceForge bug tracker as Request ID #1239906, so we won't forget.
Okay. [TRUNCATE]
Just PINGing Jonathan. I guess we should discuss this further now. And someone else had an idea to add support for (X, 0), (-X, 0) if X is of a signed type, and (0, 0) to give infinity, negative infinity, and not-a-number semantics. -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
participants (2)
-
Daryle Walker
-
Jonathan Turkanis