From: Goran on
Hi all!

When using ON_BLOCK_EXIT(_OBJ) of ScopeGuard, compiler is emitting a
warning for unused variable.

E.g. MS C++ compiler says:

warning C4189: 'scopeGuard44' : local variable is initialized but not
referenced

Clearly, that can be suppressed using #pragma warning for that
compiler. I'd like to avoid that, because:

1. I like to know about other unused variables :-),

2. better be compiler-independent, if possible.

3. if I do use #pragma warning(disable:4189) and #pragma warning
(default:4189), the "default" pragma must be outside the function
scope for it to work (not even outside scope, but outside __function__
scope, tsk, tsk, MS).

So...

Any idea of how to do that?

TIA,

Goran.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Gert-Jan de Vos on
On Dec 4, 8:01 pm, Goran <goran.pu...(a)gmail.com> wrote:
> Hi all!
>
> When using ON_BLOCK_EXIT(_OBJ) of ScopeGuard, compiler is emitting a
> warning for unused variable.

Depending on your specific ON_BLOCK_EXIT definition, you could change
the definition of ON_BLOCK_EXIT to something like:

#define ON_SCOPE_EXIT(f) scope_guard ANONYMOUS_VARIABLE(guard__) =
make_guard(f); static_cast<void>(ANONYMOUS_VARIABLE(guard__))

This works for me on VS2005

For gcc this may work:

#define ON_SCOPE_EXIT(f) scope_guard ANONYMOUS_VARIABLE(guard__)
__attribute__ ((unused)) = make_guard(f)


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Dec 6, 3:32 pm, Gert-Jan de Vos <gert-
jan.de....(a)onsneteindhoven.nl> wrote:
> On Dec 4, 8:01 pm, Goran <goran.pu...(a)gmail.com> wrote:
>
> > Hi all!
>
> > When using ON_BLOCK_EXIT(_OBJ) of ScopeGuard, compiler is emitting a
> > warning for unused variable.
>
> Depending on your specific ON_BLOCK_EXIT definition, you could change
> the definition of ON_BLOCK_EXIT to something like:
>
> #define ON_SCOPE_EXIT(f) scope_guard ANONYMOUS_VARIABLE(guard__) =
> make_guard(f); static_cast<void>(ANONYMOUS_VARIABLE(guard__))
>
> This works for me on VS2005
>
> For gcc this may work:
>
> #define ON_SCOPE_EXIT(f) scope_guard ANONYMOUS_VARIABLE(guard__)
> __attribute__ ((unused)) = make_guard(f)

I just got around to trying this.

Unfortunately, your idea works only if you guard something with a
function with no arguments. My ScopeGuard is like this (I think that's
exactly what's in CUJ article where it appeared:

#define ON_BLOCK_EXIT ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) =
MakeGuard

I am already using ON_BLOCK_EXIT with multiple parameters and with
ByRef, e.g. like so:

ON_BLOCK_EXIT(&MyResourceDisposal, param1, ByRef(param2))

But thanks for an effort!


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Boris Rasin on
On Dec 4, 9:01 pm, Goran <goran.pu...(a)gmail.com> wrote:
> Hi all!
>
> When using ON_BLOCK_EXIT(_OBJ) of ScopeGuard, compiler is emitting a
> warning for unused variable.
>
> E.g. MS C++ compiler says:
>
> warning C4189: 'scopeGuard44' : local variable is initialized but not
> referenced
>
> Clearly, that can be suppressed using #pragma warning for that
> compiler. I'd like to avoid that, because:
>
> 1. I like to know about other unused variables :-),
>
> 2. better be compiler-independent, if possible.
>
> 3. if I do use #pragma warning(disable:4189) and #pragma warning
> (default:4189), the "default" pragma must be outside the function
> scope for it to work (not even outside scope, but outside __function__
> scope,tsk,tsk, MS).
>
> So...
>
> Any idea of how to do that?

Perhaps __pragma(warning) keyword would work instead of #pragma
warning.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]