Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 1999-11-05 10:36:28


> But this misses the important case where the programmer knows from context
> that range checking is unnecessary, and wants to avoid the overhead.

I agree that some programmers may want a cast that does a numeric conversion
without any runtime checking. I recommend that we don't add anything to
casts.hpp for that. However, you might want to consider these three options
(for an individual programmer or for the Boost library) to better understand
the issue:

    1) Create yet another a cast that specifically avoids the implicit
numeric conversion warning. Such a cast be done without compiler-specific
pragmas:

        template<typename Target, typename Source>
        inline Target unchecked_numeric_cast(Source arg)
        {
            // typedefs that act as compile time assertions
            // (to be replaced by boost compile time assertions
            // as and when they become available and are stable)
            typedef bool argument_must_be_numeric
                [arg_traits::is_specialized];
            typedef bool result_must_be_numeric
                [result_traits::is_specialized];
            return static_cast<Target>(arg);
        }

    This does suffer from the "too many flavors of cast" problem that Paul
Moore mentioned.

    2) Create a form of numeric_cast that uses asserts instead of an
exception. In an earlier message on this list I suggested that some people
who don't like runtime checking in their released code might want a variant
of the entire cast.hpp file that asserts instead of throwing exceptions.
    It's worth noting that someone who prefers exceptions (left even in
released code) to asserts (debug versions only) would not want to use the
current version of polymorphic_downcast, since polymorphic_cast does the
same conversion and raises an exception on failure instead of asserting. If
we had both assert and exception version of cast.hpp, polymorphic_downcast
would be a synonym for polymorphic_cast in the exception version.
    Perhaps there are some people who want both exception and assert flavors
of the casts. Once again we run into "too many flavors of cast".

    3) Request that Boost perpetuate the half-working attempt to turn off
the warnings in implicit_cast, continuing to leave it more powerful than
standard implicit conversions. Give implicit_cast the power to convert a
double to a float even if the programmer has configured the compiler to make
an implicit conversion of double to float an error when compiling with
Visual C++. Make further attempts to do the same for CodeWarrior, gcc, and
other compilers.

    -- Darin


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