|
Boost : |
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2001-05-01 13:14:15
I have investigated this further but see snags.
namespace boost
{
namespace math
{
// Example from // B Stroustrup ISBN 0 201 70073 5, p 854
template<class T> class X
{
static T d; // Must be separately defined.
// static T const d; // const not allowed by MSVC
};
template<class T>T X<T>::d = 0; // template NOT specialised.
template<>int X<int>::d = 0; // Specialised for int.
// Possible prototype for math constants:
template<class T> class constants
{
static T pi; // const not allowed by MSVC 6
}; // template constants
template<class T>T constants<T>::pi = (T)3.1459L; // Not specialised.
template<>double constants<double>::pi = (double)3.1459; // Is
specialised for double.
}; // namespace math
}; // namespace boost
The static T constant can't be made const, so anyone can write
boost::math::constants<double>::pi = 999; // Aaargh!!!!
and it happens too!
Specialising for const double (float and long double) has the desired
effect,
but not if an unspecialised template is also defined (not a problem).
template<>const float constants<const float>::pi = 3.1459F; // Is
specialised for float.
template<>const double constants<const double>::pi = 3.1459; // Is
specialised for double.
template<>const long double constants<const long double>::pi = 3.1459L; //
Is specialised for long double.
So you have to write
std::cout << "Pi is " << boost::math::constants<const double>::pi << endl;
and not just <double>::pi or you get a link failure:
test static template.obj : error LNK2001: unresolved external symbol
"private: static double boost::math::constants<double>::pi"
(?pi@?$constants_at_N@math_at_boost@@0NA)
namespace boost
{
namespace math
{
// Example from // B Stroustrup ISBN 0 201 70073 5, p 854
template<class T> class X
{
static T d; // Must be separately defined.
// static T const d; // const not allowed by MSVC
};
template<class T>T X<T>::d = 0; // template NOT specialised.
template<>int X<int>::d = 0; // Specialised for int.
// Possible prototype for math constants:
template<class T> class constants
{
static T pi; // const not allowed by MSVC 6
}; // template constants
template<class T>T constants<T>::pi = (T)3.1459L; // Not specialised.
template<>double constants<double>::pi = (double)3.1459; // Is
specialised for double.
}; // namespace math
}; // namespace boost
But finally I don't see how to produce an acceptably abbreviated way of
presenting pi
using typedefs and/or using statements.
Effectively
using boost::math::constants<const double>::pi; // error, of course!
so the user can write:
double area = pi * r * r;
Suggestions please from language whizzos!!
Paul
> -----Original Message-----
> From: Ed Brey [mailto:brey_at_[hidden]]
> Sent: Friday, April 27, 2001 2:11 PM
> To: boost_at_[hidden]
> Subject: Re: [boost] Math Constants Library formal review results
>
>
> From: <k.hagan_at_[hidden]>
> > I'm losing track of what has and hasn't been suggested. Have we
> > considered specialising a class template with a static member?
> >
> > template<class T> class math
> > {
> > static T const pi;
> > };
>
> I don't remember it being mentioned on the list, but I experimented with
> it a while back. A problem I noticed is that the value cannot be
> specified inline in the template definition, causing most compilers to
> be unable to inline the constant in code or perform constant folding.
> Also, there is the question of whether a linker can exclude unused
> constants from the executable. None of these are issues when using
> inline functions.
>
> On the other hand, using a constant for a constant instead of a function
> feels more natural. The lure of inline functions comes from a practical
> matter addressing today's compilers' limitations. Is there any
> realistic hope that compilers before too long would be able to handle a
> member function or constant equally well, such that the boost library
> could be practical in specifying a "cleaner" interface? I noticed that
> gcc supports an extension allowing definition of a static const
> float-point type within the class definition, just like what allowed
> with ints. However, I'm not familiar enough with gcc to know how to
> easily examine the code it generates to see what does with code that it
> tuned to make use of its extension.
>
>
>
> To unsubscribe, send email to: <mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk