473,795 Members | 3,441 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
11 2972
aiooua wrote:

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.
The ISO C++ standard does not mandate "warnings" or "errors". Certain
conditions require a diagnostic. A warning is a diagnostic. It is quite
possible for a compiler to decide to continue with compilation,
especially if operating in a non-conforming mode so that it applies
various extensions.

Brian
Apr 11 '07 #11
On Apr 11, 6:58 am, "aiooua" <aio...@gmail.c omwrote:
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.
You mean that the line following the #define was actually on the
same line?

The reason I asked is because when I added the \ to the end of
the line with the #define, the code worked as expected (i.e.
failed to compile). (Of course, without the \, it failed to
compile as well.)
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
--
If I add the \ to the end of the line, I get:
cerr.cc: In function 'int main()':
cerr.cc:5: error: invalid application of 'sizeof' to a function
type
from g++. Both with no options, and with my usual options.
Maybe you have set some option somewhere to make g++ treat this
as a warning only.

--
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 12 '07 #12

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
3008
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
9931
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
2116
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
3131
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
1551
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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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
10214
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 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...
0
9042
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
6780
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
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
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
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.