Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++ > Newsgroup comp.language.c++.moderated

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-04-2005, 10:07 AM
lfeiman888@gmail.com
Guest
 
Posts: n/a
Default something about compile time assertions in Modern C++ Design

Hi,All
the following codes are from chapter 2 of Modern C++ Design,

template <boo> struct CompileTimeChecker
{
CompileTimeChecker(...);
};

template<> struct CompileTimeChecker<false> {};

#define STATIC_CHECK(expr,msg) \
{\
class ERROR_##msg {} ; \
(void)sizeof(CompileTimeChecker<(expr) != 0>(ERROR_##msg())));\
}


why there use sizeof(...)
not just

#define STATIC_CHECK(expr,msg) \
{\
class ERROR_##msg {} ; \
CompileTimeChecker<(expr) != 0>(ERROR_##msg()));\
}

thanks all in advance.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-05-2005, 10:35 AM
Zara
Guest
 
Posts: n/a
Default Re: something about compile time assertions in Modern C++ Design

On 4 Nov 2005 06:07:36 -0500, "lfeiman888@gmail.com"
<lfeiman888@gmail.com> wrote:

>Hi,All
> the following codes are from chapter 2 of Modern C++ Design,
>
>template <boo> struct CompileTimeChecker
>{
> CompileTimeChecker(...);
>};
>
>template<> struct CompileTimeChecker<false> {};
>
>#define STATIC_CHECK(expr,msg) \
>{\
> class ERROR_##msg {} ; \
> (void)sizeof(CompileTimeChecker<(expr) != 0>(ERROR_##msg())));\
>}
>
>
>why there use sizeof(...)
>not just
>
>#define STATIC_CHECK(expr,msg) \
>{\
> class ERROR_##msg {} ; \
> CompileTimeChecker<(expr) != 0>(ERROR_##msg()));\
>}
>
>thanks all in advance.
>
>


The second creates a local variable, with all code and overhead
implied.
The first checks the expression, but is optimized away not generating
any code at all, thus limiting its effects only to compile time.

Regards,

-- Zara

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 11-05-2005, 11:37 AM
Lars
Guest
 
Posts: n/a
Default Re: something about compile time assertions in Modern C++ Design

Hi,

> why there use sizeof(...)
> not just
>
> #define STATIC_CHECK(expr,msg) \
> {\
> class ERROR_##msg {} ; \
> CompileTimeChecker<(expr) != 0>(ERROR_##msg()));\
> }


As far as I remember the reason for the sizeof is to just evaluate the
type of the expression - which is why this line exists. Not using it
would create a temporary object. This is less efficient. I think the
term "type deduction engine" or something similar has been used in the
chapter regarding sizeof - but I am not sure if I read it somewhere
else.

regards

Lars


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 11-07-2005, 09:49 AM
ybjiang@winbond.com.tw
Guest
 
Posts: n/a
Default Re: something about compile time assertions in Modern C++ Design


Lars wrote:
> Hi,
>
> > why there use sizeof(...)
> > not just
> >
> > #define STATIC_CHECK(expr,msg) \
> > {\
> > class ERROR_##msg {} ; \
> > CompileTimeChecker<(expr) != 0>(ERROR_##msg()));\
> > }

>
> As far as I remember the reason for the sizeof is to just evaluate the
> type of the expression - which is why this line exists. Not using it
> would create a temporary object. This is less efficient. I think the
> term "type deduction engine" or something similar has been used in the
> chapter regarding sizeof - but I am not sure if I read it somewhere
> else.
>


Hello,

>From C98 standard clause 5.3.3:

The operand is either an expression, which is not evaluated, or a
parenthesized typeid.

All expressions will not be evaluated, sizeof operator concerns type
instead of value.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Macro quoting essentials toby dunn Newsgroup comp.soft-sys.sas 1 12-08-2005 05:41 PM
Re: sas Performance Enhancement Paul M. Dorfman Newsgroup comp.soft-sys.sas 0 10-14-2005 01:54 AM
Re: Slpit data Paul M. Dorfman Newsgroup comp.soft-sys.sas 0 08-29-2005 04:41 AM
Re: Slpit data nospam@HOWLES.COM (Howard Schreier Newsgroup comp.soft-sys.sas 0 08-29-2005 12:45 AM
Re: Adding Records for Missing Time Dennis Diskin Newsgroup comp.soft-sys.sas 0 01-18-2005 07:20 PM



All times are GMT. The time now is 11:17 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.