|
Boost : |
Subject: Re: [boost] [scope_exit] MSVC error C2355: 'this' : can only be referenced inside non-static member functions
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-08-25 20:03:34
On Wed, Aug 25, 2010 at 7:49 PM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
> Hello all,
>
> 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.
>
> The following code works on GCC but not on MSVC... This appears to be
> a MSVC 2008 bug:
> http://connect.microsoft.com/VisualStudio/feedback/details/331418/erroneous-error-c2355-this-can-only-be-referenced-inside-non-static-member-functions
>
> 1) Can anyone please try this code on MSVC versions later than 2008 to
> see if it was fixed?
> 2) Do you know of a workaround for this problem?
>
> // File: lf03.cpp
>
> #include <boost/scope_exit.hpp>
>
> struct c {
> void f() {
> typedef void (*this_t)(int this_ );
>
> #if defined(BOOST_MSVC) // for MSVC
> typedef boost::type_of::msvc_typeid_wrapper<
> sizeof(*boost::type_of::encode_start(
> #else // for GCC
> typedef __typeof__(
> boost::type_of::ensure_obj(
> #endif
> boost::scope_exit::aux::wrap(
> boost::scope_exit::aux::deref(
> this, (this_t)0
> )
> )
> )
> #if defined(BOOST_MSVC) // for MSVC
> )>::type
> #else // for GCC
> )
> #endif
> this_w;
> }
> };
>
> int main() {
> c cc;
> cc.f();
> return 0;
> }
>
> I get this error on MSVC 2008 but the code compiles just fine on GCC:
>
> Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
> Copyright (C) Microsoft Corporation. All rights reserved.
>
> lf03.cpp
> using native typeof
> lf03.cpp(17) : error C2355: 'this' : can only be referenced inside
> non-static member functions
> lf03.cpp(22) : error C2955: 'boost::type_of::msvc_typeid_wrapper' :
> use of class template requires template argument list
> C:\Program
> Files\boost\boost_1_42\boost/typeof/msvc/typeof_impl.hpp(212) : see
> declaration of 'boost::type_of::msvc_typeid_wrapper'
>
> Thank you very much.
>
> --
> Lorenzo
>
BTW, the point of this code is to automatically determine the type of
`this`. However, all the workarounds I found of the Internet assume to
know the type `c*` of this by doing:
struct c {
void f() {
c& self = *this;
... // Use `self` instead of `this` in the type determination
code -- but I already know the type `c` since I used it above...
}
};
so I cannot use these type of workarounds.
-- Lorenzo
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk