Boost logo

Boost :

From: Paul A. Bristow (boost_at_[hidden])
Date: 2003-06-24 11:20:29


The good news is that I can get this suggestion to compile using MSVC 7.1 by
adding a cast to resolve ambiguity

template<class T>
inline T area(T radius)
{
        return (T)math::pi * radius * radius;

        // (T)math::pi required to avoid ambiguity
        // std::cout << "Area (float) = " << area(1.0f) << std::endl;
        //Sletteboe_constants1.cpp(35) : error C2593: 'operator *' is ambiguous
        // could be 'built-in C++ operator*(float, float)'
        // or 'built-in C++ operator*(int, float)'
        // or 'built-in C++ operator*(double, float)'
        // or 'built-in C++ operator*(long double, float)'
        // while trying to match the argument list '(const boost::math::pi_type,
float)'
        // Sletteboe_constants1.cpp(40) : see reference to function template
instantiation 'T area<float>(T)' being compiled
        // with
        // [
        // T=float
        // ]
        // so added explicit(T)
}

This seems to say that, like me, MSVC compiler writers don't know what Gennaro
Prota does, and told/reminded us recently:

"Now, if you use the binary operators, like in:

 pi * f;

with f being a float then the selected specialization is float; i.e.
the constant "takes the type" of its adjacent operand (left operand if it
exists, right operand otherwise)."

(Though sometimes one might prefer to do the calculation using double and only
finally truncate to float - but this does what one would expect. Does

        return static_cast<T>((double)math::pi * radius * radius);

do what might avoid overflow in other circumstances?)

I also find that the explicit constructor is NOT required (even if 'strict') in:

                const struct pi_type
                {
                        //pi_type() {} // Explicit Constructor not needed for MSVC 7.1
                        template<class T>
                                operator T() const;
                }
                pi; // name

What does Intel 7.1 want?

BUT I REALLY don't like having to type (float)math::pi each time. How can this
be avoided? Users will expect to be able to just type pi or equations will be
most confusing to read, and tiresome to write.

Paul

Paul A Bristow, Prizet Farmhouse, Kendal, Cumbria, LA8 8AB UK
+44 1539 561830 Mobile +44 7714 33 02 04
Mobile mailto:pabristow_at_[hidden]
mailto:pbristow_at_[hidden]

PS The Bad News is that in wrongly manually expanding a macro in order to try to
understand what and how this is working I caused the IDE to hang during
editing - to my total astonishment :-((

So this may be too advanced a solution for MSVC 7.1 ?

| -----Original Message-----
| From: boost-bounces_at_[hidden]
| [mailto:boost-bounces_at_[hidden]]On Behalf Of Terje Slettebø
| Sent: Sunday, June 22, 2003 2:33 AM
| To: Boost mailing list
| Subject: Re: [boost] Math Constants Formal Review
|
| --- Start ---
|
| #include <iostream>
|
| #define BOOST_DEFINE_MATH_CONSTANT(name)\
| const struct name##_type\
| {\
| name##_type() {}\
| template<class T>\
| operator T() const;\
| } name;
|
| #define BOOST_MATH_CONSTANT_VALUE(name, type, value)\
| template<>\
| name##_type::operator type() const { return value; }
|
...

|<snip>

| --- End ---
|
| Output:
|
| Area (float)=3.1
| Area (double)=3.14
| Area (long double)=3.141
| PI=3.1, 3.14, 3.141

| Terje

|


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk