Boost logo

Boost :

From: Mattias Flodin (flodin_at_[hidden])
Date: 2002-10-31 13:41:40


On Thu, Oct 31, 2002 at 08:26:44AM -0600, Bill Seymour wrote:
> I've gone back to binding rounding to operators instead of operands.
> Dave Abrahams suggested a cleaner syntax (although we were talking
> about something else at the time). I think it's as exciting as
> Duff's device (I'll let you decide how exciting that is), so I've
> called it "Abrahams' apparatus." 8-)

Looking at the apparatus gave me an idea for an improvement. For this
particular library, it would probably just complicate matters for no
gain, but it may find some use in other circumstances:

namespace detail {
    struct round_down_tag {};
        struct round_up_tag {};
}
namespace {
    const detail::round_down_tag round_down;
        const detail::round_up_tag round_up;
}

We can now make two separate overloads of operator[] that hold different
return types:

detail::round_down_proxy operator[](detail::round_down_tag);
detail::round_up_proxy operator[](detail::round_up_tag);

This way di[round_down] and di[round_up] would invoke different
functions. The method has two advantages. The first is a perfomance gain
that may become significant if the operator runtime is of the same
magnitude as an integer comparison + branch. The second is that of
template instantiations: for the particular parameterization, round_down
may be illegal but round_up isn't. Using round_down would then cause a
compile-time error whereas round_up wouldn't.

-- 
Mattias Flodin <flodin_at_[hidden]>  -  http://www.cs.umu.se/~flodin/
Room NADV 102
Department of Computing Science
Umeå University
S-901 87 Umeå, Sweden
--
"Too many errors on one line (make fewer)"
  -- Error message from Apple's MPW C compiler

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