|
|||
|
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! ] |
|
|
||||
|
||||
|
|
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |