|
Boost : |
Subject: [boost] [scope_exit] Boost.ScopeExit capturing 'this'
From: adam.butcher_at_[hidden]
Date: 2009-02-27 06:40:46
I wasn't sure whether I had the correct address for Boost.ScopeExit's
author Alexander Nasonov, so I've submitted to this list just in case and
to get feedback from other users.
I have been playing with BOOST_SCOPE_EXIT -- it it very convenient.
One thing I would have liked would be to capture 'this' inside member
functions. I have created a patch which adds BOOST_SCOPE_EXIT_THIS to
do just that. It provides an operator->() in the generated struct
which can be used to access the enclosing class's members.
The syntax is:
BOOST_SCOPE_EXIT_THIS( (&a) (&b) )
{
a; b; // local captures
(*this)->member; // member access
}
BOOST_SCOPE_EXIT_END;
and can be
BOOST_SCOPE_EXIT_THIS()
{
(*this)->member;
}
BOOST_SCOPE_EXIT_END;
providing the preprocessor allows for empty parameter lists (which gcc
does and vc does albeit after disabling a warning to remove noise).
The type pointed to by the result of operator->() is correctly cv
qualified.
It has been tested on gcc 4.3, gcc 4.2, msvc 7.1 and msvc 9.0. I don't
have access to other compilers.
Boost.Typeof worked fine on gcc for typeof(this) -- mostly because gcc
has a native typeof I guess.
On visual studio (vc71 and vc90) it was not so good -- a compiler bug
(
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=331418
,
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=365549
)
that has been marked as 'Won't fix' means that 'this' cannot be used
in expressions within template argument lists -- even if those
expressions are innocuous typeid()s or sizeof()s for type-inference.
The common workaround (capturing a compile-time integer with an
enumerator and using the enumerator in the template parameter list)
doesn't work with the vc71-specific implementation of Boost.Typeof
since it is based on typeid() -- this cannot be captured in a
compile-time expression (or I know of no way to that doesn't involve
the expression existing in a template argument list).
All is not lost though -- the general-purpose sizeof() based approach
for the other VC versions works fine for VC71 for determining the
typeof 'this'. As such my patch includes a VC>=1300 specific
workaround for typeof 'this' determination. This is unfortunate but I
couldn't think of any other non-intrusive way of doing it.
Please find the attached patch and let me know what you think -- I'm
sure this feature would be useful to users -- it saves having to do:
TypeOfMyClassWhichCouldBeTedious* self = this
BOOST_SCOPE_EXIT( (&self) )
{
self->member;
}
BOOST_SCOPE_EXIT_END;
which removes the convenience of BOOST_SCOPE_EXIT in these contexts
since something like:
struct at_end { TypeOfMyClassWhichCouldBeTedious* self; ~at_end()
{
self->member;
}
} at_end = {this};
can be done without any dependencies to achieve the same result.
Regards,
Adam.
------------------------------------------------------------
This email and any attached files contains company confidential information which may be legally privileged. It is intended only for the person(s) or entity to which it is addressed and solely for the purposes set forth therein. If you are not the intended recipient or have received this email in error please notify the sender by return, delete it from your system and destroy any local copies. It is strictly forbidden to use the information in this email including any attachment or part thereof including copying, disclosing, distributing, amending or using for any other purpose.
In addition the sender excludes all liabilities (whether tortious or common law) for damage or breach arising or related to this email including but not limited to viruses and libel.
SELEX Communications Limited is a Private Limited Company registered in England and Wales under Company Number 964533 and whose Registered Office is Lambda House, Christopher Martin Rd, Basildon, SS14 3EL. England.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk