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 12:44:46


Adam Butcher-2 wrote:
>
> On Tue, March 1, 2011 4:38 pm, lcaminiti wrote:
>>
>>>>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
>>>(http://lists.boost.org/Archives/boost/2009/03/149540.php) 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>
>>>
>>>You should be able to use the same type index trick in localfunction.
>>>The key definition is the function-style macro
>>>BOOST_SCOPE_EXIT_TYPEDEF_TYPEOF_THIS().
>>
>> Why is this patch defining the following accessors for the ScopeExit
>> local
>> struct?
>>
>> [snip]
>>
>> 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->`:
>>
>> [snip]
>>
>> Am I not understanding the purpose of the patch's assessors correctly?
>>
> At the time the suggest syntax for accessing the enclosing class'
> this-pointer was the expression
>
> (*this)
>
> So from within the scope-exit you would use (*this) as the object of the
> enclosing class (rather than plain 'this' which refers to the instance
> of the scope-exit class). Though contentious, this was my best effort
> at the time to get the word 'this' into member accesses. Other
> suggestions IIRC where calling explicit functions such as
> enclosing_this()-> or this_()-> or having those names as pointer members
> (i.e. enclosing_this->, or this_->).
>
> The reason for the implicit conversion to reference-to the enclosing
> instance was to allow passing of (*this) to functions that expected a
> pointer or reference to the enclosing class.
>

OK, I understand now. I am using `this_` in my application (Boost.Local) and
I will keep that syntax.

(FYI, there is a mechanism to use the plain `this` to assess the bound
object but it relies on an unsafe `static_cast<>` -- see
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/d3a86f27277f713b)

Thanks a lot for the clarification!

--
Lorenzo
-- 
View this message in context: http://boost.2283326.n4.nabble.com/scope-exit-MSVC-error-C2355-this-can-only-be-referenced-inside-non-static-member-functions-tp2638909p3330249.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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