Boost logo

Boost :

Subject: Re: [boost] [local_function] how bad is this `static_cast<>`?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-09-12 10:33:04


On Sun, Sep 12, 2010 at 5:54 AM, John Bytheway
<jbytheway+boost_at_[hidden]> wrote:
> On 11/09/10 19:52, Lorenzo Caminiti wrote:
>> I almost wish Boosters could conclude they will not want/accept the
>> hack and they are willing to use `this_` so to make
>> Boost.LocalFunction implementation easier :) .
>>
>> *** Please advice -- or ask questions if you need more information in
>> order to advice. ***
>
> I think requiring this_ is fine.  I was a bit worried about the

Do other Boosters share the same opinion?

> possibility of silent failure when people forget (quite likely to happen
> during refactoring) but luckily there are very few circumstances I can
> thing of where it wouldn't give a compile error when you accidentally
> use this (the two that spring to mind are printing out the value, and
> reinterpret_casting it).  Still, if there were a way to make it
> impossible to use 'this' at all, that would be nice to avoid this risk
> (but I doubt you can).

Actually, if the CONFIG macro is left #undef (default) then `this_`
must be used and the local function is implemented as a static
function so `this` cannot be used at all (not even by mistake). For
example, the code below generates this error on GCC:

$ g++ -Wall -Werror -I./src example/local_function_/line.cpp
example/local_function_/line.cpp: In static member function ‘static
double c::f(double)::contractXlocal_function_functorXlineX::contractXlocal_function_castableX::contractXlocal_function_bodyX(int,
const double&, int, c*)’:
example/local_function_/line.cpp:27: error: ‘this’ is unavailable for
static member functions

struct c {
    c(): y_(0.0) {}

    double f(double x) {
        int offset = x > 0 ? +100 : -100;

        CONTRACT_DETAIL_LOCAL_FUNCTION(
        (double) (line)( (int)(slope)
                (const bound)((&x)(offset)) (bound)((this)) )
        ) {
            double y = slope * x + offset;
            return this->y_ = y; // ERROR: `this` not available (must
use `this_` instead).
        } CONTRACT_DETAIL_LOCAL_FUNCTION_END(line)

        return line(2);
    }

private:
    double y_;
};

--
Lorenzo

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