Cast to void???

D

Dave Theese

In Loki (smartptr.h), I've noticed the following expression occurring by
itself:

(void)val;

What is the purpose in casting something to void, especially in a
stand-alone expression such as this and not as part of something larger?
I'm surprised this is possible as it is not possible to declare a variable
of type void. And I'm intrigued to learn the purpose...

Here's the context I see this in:

static void OnDereference(P val)
{ assert(val); (void)val; }

BTW, I see this in the VC++ port of Loki. I don't know if it's in the
original Loki or not...

Thanks,
Dave
 
W

WW

Dave said:
In Loki (smartptr.h), I've noticed the following expression occurring
by itself:

(void)val;

What is the purpose in casting something to void, especially in a
stand-alone expression such as this and not as part of something
larger? I'm surprised this is possible as it is not possible to
declare a variable of type void. And I'm intrigued to learn the
purpose...
[SNIP]

First time I see this and the only reason I can imagine is to avoid compile
time warnings for unused variables.
 
J

John Almer

Dave said:
In Loki (smartptr.h), I've noticed the following expression occurring
by itself:

(void)val;

What is the purpose in casting something to void, especially in a
stand-alone expression such as this and not as part of something
larger? I'm surprised this is possible as it is not possible to
declare a variable of type void. And I'm intrigued to learn the
purpose...
[SNIP]

First time I see this and the only reason I can imagine is to avoid compile
time warnings for unused variables.

No need for this however. An unused parameter name can simply be omitted.
 
W

WW

John Almer wrote:
[SNIP]
No need for this however. An unused parameter name can simply be
omitted.

Yes. But did I talk about a paramneter?

It is deep inside template metaprogramming magic. I can easily imagine it
was some sort of variable not used in a function in certain cases.
 
J

John Almer

No need for this however. An unused parameter name can simply be
Yes. But did I talk about a paramneter?

No, but the op did.
It is deep inside template metaprogramming magic. I can easily imagine it
was some sort of variable not used in a function in certain cases.

Probably. However, templates aren't "magic". They generate ordinary code no
different than hand-written code. And no self-respecting programmer would
ever use such a technique. However, there are no absolutes in programming
and things aren't always so cut and dry (especially where metaprogramming is
concerned). So I would never rule out the need for this type of construct
under some very rare circumstance. It's obfuscating however and more likely
than not a sign of bad design (in most cases anyway).
 
W

WW

John said:
No, but the op did.


Probably. However, templates aren't "magic".

No, they aren't. What I was talking about is template metaprogramming
magic. A term of 3 words.
They generate ordinary code no different than hand-written code.

Really? How about two phase name lookup? Dependent names? Not being
compiled until instantiated? Compile time programming using template
metaprogramming being a Turing capable language?
And no self-respecting
programmer would ever use such a technique.

What technique????
However, there are no
absolutes in programming and things aren't always so cut and dry
(especially where metaprogramming is concerned). So I would never
rule out the need for this type of construct under some very rare
circumstance. It's obfuscating however and more likely than not a
sign of bad design (in most cases anyway).

You should not rule it out. Since the code (whiuch I have appartenly missed
to read carefully at first) looks like this:

static void OnDereference(P val)
{ assert(val); (void)val; }


How will you program the assert without a name for the argument???

How will you avoid the warning when the argument is not used in production
builds since the assert is preprocessed to nothing?
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

John Almer escribió:
No need for this however. An unused parameter name can simply be omitted.

The parameter is used by the assert, then can't be omitted. The warning
about para meter unused will come in the non debug compilation.

Regrads.
 
J

John Almer

They generate ordinary code no different than hand-written code.

Yes, really. You're confusing the mechanics of templates with the code it
produces. A template is just a boilerplate for ordinary code. It doesn't
force you to write unnecessary or extraneous constructs.
What technique????

Casting to a void to eliminate a compiler warning.
You should not rule it out. Since the code (whiuch I have appartenly missed
to read carefully at first) looks like this:

static void OnDereference(P val)
{ assert(val); (void)val; }


How will you program the assert without a name for the argument???
How will you avoid the warning when the argument is not used in production
builds since the assert is preprocessed to nothing?

#ifdef NDEBUG
static void OnDereference(P)
{
#else
static void OnDereference(P val)
{
assert(val);
#endif

// ...
}

While certainly more verbose (and there are some variations on this), it
makes it a lot clearer that the arg isn't used in production. Had it been
done this way in the first place then the op would have either recognized it
or asked about unused (nameless) parameters instead.
 
W

WW

John said:
Yes, really. You're confusing the mechanics of templates with the
code it produces.

I have never talked about code generated by templates.
A template is just a boilerplate for ordinary code.

Ordinary code. You mean ordinary code (template metaprogramming we talk
about) which might never appear in the final executable and is really
executed runtime?
It doesn't force you to write unnecessary or extraneous constructs.

Where did I say that?
Casting to a void to eliminate a compiler warning.

There was no other way. BTW I would love to see what would Andrei's
reaction to be on that. He is self-respecting. :)
#ifdef NDEBUG
static void OnDereference(P)
{
#else
static void OnDereference(P val)
{
assert(val);
#endif

// ...
}

Holy shit! Turn your code into an ifdef madness just because your knee-jerk
reaction does not like casting to void?
While certainly more verbose

And breaking the once and only once rule...

And a call for maintenance nightmare...

And ugly...
(and there are some variations on this),

Please do not show them.
it makes it a lot clearer that the arg isn't used in production.

Not for me. For me it makes it messy and unreadable and highly error prone
due to duplication.
Had
it been done this way in the first place then the op would have
either recognized it or asked about unused (nameless) parameters
instead.

Had it been written this way ogirinally I would have mailed Andrei asking
hom why does he write ugly code and duplicated when he does not need to.
And it wasn't only me mailing him.

Loki internals are not for the faint hearthed or the beginner. For me it
took less than a second to figure out what it means - and I am not a guru.
And let me change your sentence: did the OP look it up what assert is he
could have deduced the whole idea himself. As I did. It helps if you
think.
 
K

Kevin Goodsell

Dave said:
In Loki (smartptr.h), I've noticed the following expression occurring by
itself:

(void)val;

This is sometimes used to silence warnings from static code analysis
tools (like LINT), but I don't know if that's the reason for it here.
Silencing compiler warnings seems more likely.

-Kevin
 
J

Jacek Dziedzic

This might be used to kill the compiler warning "unused variable
val" when the assert() is switched off (in a release compilation).
I used to use things like

val=val;
or
if(0) val=val;

for this purpose.

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Lambda and cast to pointer to void 1
Cast to void * and back. 10
Possible workaround for dlsym() and cast from/to void* 3
Is this void* cast safe? 9
void()const 9
cast 1
To void or not to void 3
void * 4

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top