Boost logo

Boost :

From: Michael D. Borghardt (michael_at_[hidden])
Date: 2004-01-03 18:03:12


The second test uses my template class numeric_class

I expected the first part of the test to succeed as you indicated, it is the
seond part that has me wondering.

    // I expect Test 2 to fail with a loss of range - but it does not
    std::cout << "Test 2" << std::endl;
    a = 255;
    try
    {
        numeric_class<unsigned char> uc(a);
        std::cout << "Value " << a << " OK" << std::endl;
        signed char sc;

// We are OK until here, but I would expect the next line to fail
        uc.getValue(sc); // this should convert from unsigned char to
signed char and fail

        a= sc;
        std::cout << "Value now signed " << a << " OK" << std::endl;
    }
    catch (boost::bad_numeric_cast &ex)
    {
        std::cout << "Value " << a << " " << ex.what() << std::endl;
    }

Here is my template class again

template<typename Target>
class numeric_class
{
public:
    template<typename Source>numeric_class(Source s)
    {
        value_ = boost::numeric_cast<Target>(s);
    };

    template<typename Resultant>void getValue(Resultant & r)
    {
        r = boost::numeric_cast<Resultant>(value_);
    };
private:
    Target value_;
};


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