Boost logo

Boost :

Subject: Re: [boost] [scope_exit] MSVC error C2355: 'this' : can only be referenced inside non-static member functions
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-03-01 11:38:38

>>On Thu, August 26, 2010 12:49 am, Lorenzo Caminiti wrote:
>> I am trying to use Boost.ScopeExit parameter binding mechanism to
>> implement Boost.LocalFunction. To do so, I need to use some internal
>> ScopeExit code to detect the type of `this` and bind its value to the
>> local function declaration.
>> <snip>
>> 2) Do you know of a workaround for this problem?
>I posted a workaround for this a while back
>( which used
>type indexing (where the index for typeof(this) is captured outside of a
>template parameter list) to avoid specifying 'this' in a template
>parameter list. The mechanics for the solution came from boost.typeof.
> <snip>
>I have attached my latest version of the patch which still applies fine
>to 1.44 and, I think, works on cl versions from vc7.1 upwards.
>You should be able to use the same type index trick in localfunction.
>The key definition is the function-style macro

Why is this patch defining the following accessors for the ScopeExit local

+ inline BOOST_PP_CAT(se_this_type,id) operator->() { return
boost_se_params_->se_this; } \
+ inline BOOST_PP_CAT(se_this_type,id) operator*() { return
boost_se_params_->se_this; } \
+ inline operator BOOST_PP_CAT(se_this_type,id)() { return
boost_se_params_->se_this; } \

I think these are meant to use `this` to access the bound object instead of
the local struct object. However, these accessors don't work because
`this->` (and `*this`) never calls the overloaded `operator->`:

#include <iostream>

struct obj_t {
    int x;
    int f() { return -1; }
} obj;

typedef obj_t* this_t;

int main() {
    struct l {
        void operator()() {
            std::cout << this->x << std::endl; // error :((
            std::cout << this->f() << std::endl; // error :((
            std::cout << this->operator->()->x << std::endl; // ok
            std::cout << this->operator->()->f() << std::endl; // ok
        this_t operator->() { return &obj; }
        this_t operator*() { return &obj; }
        operator this_t() { return &obj; }
        int f;
    } ll;
    return 0;

Am I not understanding the purpose of the patch's assessors correctly?

Thanks for the clarification.

View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at