Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Printing concatenated string ..

25 views
Skip to first unread message

Alf P. Steinbach

unread,
Nov 30, 2013, 4:31:59 PM11/30/13
to
[follow-up set to comp.lang.c++]

On 14.11.2013 01:20, Stefan Ram wrote:
> Barry Schwarz <schw...@dqel.com> writes:
>> This is the standard push-pop idiom for temporarily changing any state
>> variable and then restoring it.
>
> One can use RAII with a custom class �with� like:
>
> { with flags{ ::std::hex }; /* use ::std::hex here */ }
> /* flags are restored here */

There may be some problem with the following code, because for some
reason it lingered in an old repository, not moved over to current.

But anyway, people may just find it useful to sort of emulate C# "using"
or Python "with" or the new-fangled Java variant of its "try":


[code]
/*
CPPX_WITH( std::mutex() )
{
// Blah blah...
}
*/

#define CPPX_WITH( initializer ) \
if( bool cppx_with_finished = false ) {} \
else for( \
auto const& cppx_with_object = initializer; \
((void) cppx_with_object, !cppx_with_finished); \
cppx_with_finished = true )
[/code]


Explanation: the "if" is a limited means to introduce an extra local
variable, that is used to control the "for" loop which is a more general
means to introduce a local variable. The "(void)" cast makes that latter
variable appear to be used, so as to avoid silly-warnings about it. So
essentially, CPPX_WITH creates a local scope where an "anonymous"
variable is declared with the specified initializer expression, and the
whole point is usually to express more clearly that some code depends on
being executed between that variable's initialization and cleanup.

To make the macro more accurately reflect the intent of an anonymous
variable one could perhaps generate a less easy to use name via
__LINE__, but of old at least one compiler (Visual C++) messed that up
when some special option (Zi?) was used, so that that compiler had to be
detected and treated specially -- as I discovered when I first tried
out the original scope guard implementation.


Cheers & hth.,

- Alf

0 new messages