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 10 19164
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
> 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.
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
> > 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).
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
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.
> > 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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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
|
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 = (void*)(*((unsigned char**)&pData) + 4);
|
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.
// ===================================================================
#include <iostream>
using namespace std;
void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
|
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:
0x00000000 through 0xFFFFFFFE : Valid byte addresses
| |
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".
Question 28 says:
|
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 function.
The name of the functions are read in txt file at runtime.
So I want to do something like
char * func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)funct_to_run, arg);
|
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 solutions?
also this pops up: 43: error: expected unqualified-id before 'return'
code:
includes: stdio.h, stdlib.h, time.h, pthread.h
struct:
template <typename Tstruct mergesort_thread {
T* start;
|
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 with this and if there is a better
solution.
-jeff
|
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
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |