473,503 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cast to void???

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
Jul 19 '05 #1
10 19056
WW
Dave Theese wrote:
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.

--
WW aka Attila
Jul 19 '05 #2
> Dave Theese wrote:
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.
Jul 19 '05 #3
WW
John Almer wrote:
[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.


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.

--
WW aka Attila
Jul 19 '05 #4
> > No need for this however. An unused parameter name can simply be
omitted.
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).
Jul 19 '05 #5
WW
John Almer wrote:
No need for this however. An unused parameter name can simply be
omitted.
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".


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?

--
WW aka Attila
Jul 19 '05 #6
John Almer escribió:
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.


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.
Jul 19 '05 #7
> > They generate ordinary code no different than hand-written code.

Really?
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.
And no self-respecting
programmer would ever use such a technique.
What technique????


Casting to a void to eliminate a compiler warning.
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?


#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.
Jul 19 '05 #8
WW
John Almer wrote:
They generate ordinary code no different than hand-written code.
Really?


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?
And no self-respecting
programmer would ever use such a technique.


What technique????


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. :-)
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

// ...
}


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.

--
WW aka Attila
Jul 19 '05 #9
Dave Theese wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10
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.
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1396
by: William Payne | last post by:
In the following code, should the code compile without casting &n to void* ? My compiler accepts it, but should it? void foo(void*) { } struct bar { int n;
5
2986
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
9
1444
by: gvarndell | last post by:
Hi, In the following code fragment, gcc seems to ignore the initial value assigned to pData. (compiled -fvolatile -O2 with gcc) void test(void) { void *pData = (void*)0x3400; pData =...
5
2025
by: Matt | last post by:
I am trying to cast an ostream reference to void* and back again. The code below shows the problem isolated from a more complex program. It compiles quietly but seg faults upon execution. //...
9
2697
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
5
1880
by: johnbrown105 | last post by:
Hello All, I did try searching the archives, but the topics seemed to be mostly about base and derived classes. I have reached Chapter 8 in Bruce Eckel's "Thinking in C++ 2nd Edition Vol....
1
452
by: etienne | last post by:
Hello all, i'm experimenting a problem in trying to cast a char * variable to a pointer to a function. My problem is that I want to execute functions in threads, using the pthread_create...
14
4403
by: andreyvul | last post by:
g++ says a reinterpret cast from void* is disallowed; however, all I'm trying to do is de-capsulate a void* argument that holds the pointer to a class (and static_cast forces cast constructor) any...
3
10641
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
7
3937
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.