473,785 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why this code must add sizeof()?

Hello Everybody
In <<Modern C++ Design>Compile-Time Assertions
there is :
template <boolstruct CompileTimeChec ker
{
CompileTimeChec ker(...);
};

template <struct CompileTimeChec ker<false{ };

#define STATIC_CHECK(ex pr,msg) \
{ \
class ERROR_##msg {}; \
(void)sizeof( CompileTimeChec ker< (expr) !=
0>( (ERROR_##msg() ) )); \
}
//.....
so my question is why " (void)sizeof( CompileTimeChec ker< (expr) !=
0>( (ERROR_##msg() ) )); \" must add sizeof?
Thanks

Apr 9 '07 #1
11 2970
nevergone wrote:
so my question is why " (void)sizeof( CompileTimeChec ker< (expr) !=
0>( (ERROR_##msg() ) )); \" must add sizeof?
Thanks
Sizeof is a handy function used in these kinds of macros because
it causes the syntax of the expression to be checked but never
actually evaluates the expression.

Apr 9 '07 #2
On Apr 9, 2:08 pm, "nevergone" <wyx2006s...@gm ail.comwrote:
so my question is why " (void)sizeof( CompileTimeChec ker< (expr) != 0>( (ERROR_##msg() ) )); \" must add sizeof?
in general, checks for incomplete types are better done within sizeof
because it's runtime overheads are minimum. try to generate the
assembly code (using, g++ -S) with and without the "sizeof" and you'll
see that the sizeof version is better.

however, i have a related, but different problem. i tried the code as
printed in the book[#1] and couldn't get it to work.

---
/* begin code */
template<boolst ruct CompileTimeChec ker{ CompileTimeChec ker(...); };
template<struct CompileTimeChec ker<false{ };
#define STATIC_CHECK(ex pr, msg) { class ERROR_##msg {};
(void)sizeof(Co mpileTimeChecke r<(expr) != 0>((ERROR_##msg ()))); }
int main(){ STATIC_CHECK(1> 2,mustFail);}
/* end code */
---

here's what g++ gives me:

---
g++ -Wall -pedantic test.cpp
test.cpp: In function `int main()':
test.cpp:4: warning: ISO C++ forbids applying `sizeof' to a function
type
---

apart from that warning, the compilation goes through.
shouldn't it fail? what am i missing?

thanks,
--
#1 - pg. 21, Modern C++ Design, Andrei Alexandrescu.

Apr 9 '07 #3
On 4ÔÂ9ÈÕ, ÏÂÎç8ʱ34·Ö, "aiooua" <aio...@gmail.c om>wrote:
On Apr 9, 2:08 pm, "nevergone" <wyx2006s...@gm ail.comwrote:
so my question is why " (void)sizeof( CompileTimeChec ker< (expr) != 0>( (ERROR_##msg() ) )); \" must add sizeof?

in general, checks for incomplete types are better done within sizeof
because it's runtime overheads are minimum. try to generate the
assembly code (using, g++ -S) with and without the "sizeof" and you'll
see that the sizeof version is better.

however, i have a related, but different problem. i tried the code as
printed in the book[#1] and couldn't get it to work.

---
/* begin code */
template<boolst ruct CompileTimeChec ker{ CompileTimeChec ker(...); };
template<struct CompileTimeChec ker<false{ };
#define STATIC_CHECK(ex pr, msg) { class ERROR_##msg {};
(void)sizeof(Co mpileTimeChecke r<(expr) != 0>((ERROR_##msg ()))); }
int main(){ STATIC_CHECK(1> 2,mustFail);}
/* end code */
---

here's what g++ gives me:

---g++ -Wall -pedantic test.cpp

test.cpp: In function `int main()':
test.cpp:4: warning: ISO C++ forbids applying `sizeof' to a function
type
---

apart from that warning, the compilation goes through.
shouldn't it fail? what am i missing?

thanks,
--
#1 - pg. 21, Modern C++ Design, Andrei Alexandrescu.
I don't know.
but i think it would be better to add () in 1>2

Apr 9 '07 #4
nevergone wrote:
On 4ÔÂ9ÈÕ, ÏÂÎç8ʱ34·Ö, "aiooua" <aio...@gmail.c omwrote:
>On Apr 9, 2:08 pm, "nevergone" <wyx2006s...@gm ail.comwrote:
>>so my question is why " (void)sizeof( CompileTimeChec ker< (expr) != 0>( (ERROR_##msg() ) )); \" must add sizeof?
in general, checks for incomplete types are better done within sizeof
because it's runtime overheads are minimum. try to generate the
assembly code (using, g++ -S) with and without the "sizeof" and you'll
see that the sizeof version is better.

however, i have a related, but different problem. i tried the code as
printed in the book[#1] and couldn't get it to work.

---
/* begin code */
template<bools truct CompileTimeChec ker{ CompileTimeChec ker(...); };
template<struc t CompileTimeChec ker<false{ };
#define STATIC_CHECK(ex pr, msg) { class ERROR_##msg {};
(void)sizeof(C ompileTimeCheck er<(expr) != 0>((ERROR_##msg ()))); }
int main(){ STATIC_CHECK(1> 2,mustFail);}
/* end code */
---

here's what g++ gives me:

---g++ -Wall -pedantic test.cpp

test.cpp: In function `int main()':
test.cpp:4: warning: ISO C++ forbids applying `sizeof' to a function
type
---

apart from that warning, the compilation goes through.
shouldn't it fail? what am i missing?

thanks,
--
#1 - pg. 21, Modern C++ Design, Andrei Alexandrescu.
I don't know.
but i think it would be better to add () in 1>2
It's not necessary to add () in 1>2 except for readability reasons
(arguably since some people consider more () to be less readable). In
the macro, 'expr' is already guarded by ().

I found the code example confusing, it almost seemed like it should do a
sizeof(CompileT imeChecker<(exp r)>) != 0 test instead. It may be trying
to facility the empty structure optimization, an empty struct/class's
sizeof returns 0 as the specialization template<struct
CompileTimeChec ker<false{ }; would do.

Fei
Apr 9 '07 #5
i got the same problem with aiooua
but if i took off the sizeof function
the code could work,but it seems that the compiler took the code
"CompileTimeChe cker< (expr) >( (ERROR_##msg() ) )"
as a function declaration instead of a temporary object
here is my test code :

#include <iostream>
using namespace std ;
template <boolstruct CompileTimeChec ker
{
CompileTimeChec ker(...) { cout << "constructo r" << endl ; }
};
template <struct CompileTimeChec ker<false{ };
#define STATIC_CHECK(ex pr,msg) \
{ \
class ERROR_##msg {}; \
CompileTimeChec ker< (expr) >( (ERROR_##msg() ) ); \
}
int main()
{
STATIC_CHECK( true , error ) ;
return 0 ;
}
can anybody explain it ?

Apr 10 '07 #6
On Mon, 09 Apr 2007 07:51:25 -0400, Ron Natalie <ro*@spamcop.ne t>
wrote in comp.lang.c++:
nevergone wrote:
so my question is why " (void)sizeof( CompileTimeChec ker< (expr) !=
0>( (ERROR_##msg() ) )); \" must add sizeof?
Thanks
Sizeof is a handy function used in these kinds of macros because
it causes the syntax of the expression to be checked but never
actually evaluates the expression.
sizeof is NOT a function, it is an operator.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Apr 10 '07 #7
i think i got the answer:
suppose you have a class : class a { public : a(){} } ;
when you write the code : a() ;
the compiler can explain this in two ways:
1. a temporary object of type a
2. a function declaration of type a (*)(void)
so , in order to distinguish this you can use sizeof
which can not take a function type as a parameter
in order to tell sizeof that which you pass is a type
you have to add additional ()
so ,if you write sizeof( ( a() ) ) , the compiler will understand
that
a() is a temporary object

Apr 10 '07 #8
On Apr 9, 2:34 pm, "aiooua" <aio...@gmail.c omwrote:
On Apr 9, 2:08 pm, "nevergone" <wyx2006s...@gm ail.comwrote:
so my question is why " (void)sizeof( CompileTimeChec ker< (expr) != 0>( (ERROR_##msg() ) )); \" must add sizeof?
in general, checks for incomplete types are better done within sizeof
because it's runtime overheads are minimum. try to generate the
assembly code (using, g++ -S) with and without the "sizeof" and you'll
see that the sizeof version is better.
however, i have a related, but different problem. i tried the code as
printed in the book[#1] and couldn't get it to work.
---
/* begin code */
template<boolst ruct CompileTimeChec ker{ CompileTimeChec ker(...); };
template<struct CompileTimeChec ker<false{ };
#define STATIC_CHECK(ex pr, msg) { class ERROR_##msg {};
(void)sizeof(Co mpileTimeChecke r<(expr) != 0>((ERROR_##msg ()))); }
I'm not sure about the exact error message you got---it seems a
little strange. But this line is definitly illegal; for
starters, it contains a } for which there is no {. And ##
operators are only legal in macros.

Are you sure you didn't forget a \ at the end of the previous
line? This line would make sense as part of the macro.
int main(){ STATIC_CHECK(1> 2,mustFail);}
/* end code */
---
here's what g++ gives me:
---g++ -Wall -pedantic test.cpp
test.cpp: In function `int main()':
test.cpp:4: warning: ISO C++ forbids applying `sizeof' to a function
type
Which, as I say, is wierd. The only way a compiler could
possibly parse the sizeof here is as part of an expression
statement. (But of course, expression statements are not legal
at namespace scope.)

Note that if you did mean for this line to be part of the macro,
the line number in the error message should tip you off
immediately about the forgotten \. You never get error messages
from continuation lines in a macro. (Just error messages from
where you invoke the macro.)
---
apart from that warning, the compilation goes through.
shouldn't it fail? what am i missing?
Probably just an \.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 10 '07 #9
On Apr 10, 8:34 pm, "James Kanze" <james.ka...@gm ail.comwrote:
On Apr 9, 2:34 pm, "aiooua" <aio...@gmail.c omwrote:
however, i have a related, but different problem. i tried the code as
printed in the book[#1] and couldn't get it to work.
---
/* begin code */
template<boolst ruct CompileTimeChec ker{ CompileTimeChec ker(...); };
template<struct CompileTimeChec ker<false{ };
#define STATIC_CHECK(ex pr, msg) { class ERROR_##msg {};
(void)sizeof(Co mpileTimeChecke r<(expr) != 0>((ERROR_##msg ()))); }
Are you sure you didn't forget a \ at the end of the previous
line? This line would make sense as part of the macro.
the macro was well-formed when i compiled, it got wrapped into two
lines (maybe because of line-length limits or something) while i
posted it here.
I'm not sure about the exact error message you got---it seems a
unlike what the book says, and what i expected i did not get any
error.
however, i got the following warning.

--
test.cpp:4: warning: ISO C++ forbids applying `sizeof' to a function
type
--

it seemed too simple to be a g++ bug, but i'll follow it on their
newsgroup.

thanks,

Apr 11 '07 #10

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

Similar topics

1
1306
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
16
3006
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work - compiles fine, pack appears to work, but unpack retrieves jibberish and causes program to crash).
14
9930
by: nullptr | last post by:
Hi, As an exercise, I've written a program implementing run-length encoding. I would be more that happy to hear your comments, suggestions, etc Thanks, --- #include <stdio.h> #include <string.h>
11
2044
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work). I would be grateful for any feedback that helps fix this. My intention
3
2115
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work - compiles fine, pack appears to work, but unpack retrieves jibberish and causes program to crash).
18
3130
by: vermarajeev | last post by:
Hello everybody, This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query. Here is the code which I have written. #include<iostream> #include<fstream> using namespace std; class myClass
12
2409
by: syn1kk | last post by:
1: float (*data); 2: data = malloc(31 * sizeof(data)); 3: data = VARIABLE; Question 1: The variable data is a float pointer? Question 2: When the is used. Does that mean it is an array of float pointers? What does it mean? Question 3: Or does it mean that there are 16384 floats allocated with
8
1550
by: Wayne Shu | last post by:
The code is below: #include <iostream> #include <functional> template <typename T, typename FUN> void insert_sort(T *arr, size_t size, FUN f = std::less<T>()) { for( int j = 1; j < size; j++ ){ type key = arr; int i = j - 1;
9
2519
by: onkar | last post by:
how many bytes will be allocted by following code - #include<stdio.h> #define MAXROW 3 #define MAXCOL 4 int main(int argc,char **argv){ int (*p); p=(int (*))malloc(sizeof(*p)*MAXROW); printf("%d\n",sizeof(*p)); return 0;
0
9480
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,...
0
10315
Oralloy
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...
1
10083
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 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...
0
8968
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, 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...
0
6737
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();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
2
3645
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.