Connecting Tech Pros Worldwide Forums | Help | Site Map

About STATIC_CHECK macro

alex
Guest
 
Posts: n/a
#1: Jul 23 '05
I read famous book "Modern C++ Design". In chapter 2, there is a
STATIC_CHECK sample:
--------------------------------------------------
template < bool >
struct CompileTimeChecker
{
CompileTimeChecker(...);
};

template <>
struct CompileTimeChecker< false >
{};

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

---------------------------------------------------------
I use STATIC_CHECK, but get a error:
ISO C++ forbids applying `sizeof' to a function type

Why this error? Why use sizeof?

thanks!


Rapscallion
Guest
 
Posts: n/a
#2: Jul 23 '05

re: About STATIC_CHECK macro


alex wrote:[color=blue]
> I read famous book "Modern C++ Design". In chapter 2, there is a
> STATIC_CHECK sample:[/color]

Try this instead:
http://www.jaggersoft.com/pubs/CVu11_3.html
http://www.jaggersoft.com/pubs/CVu11_5.html

Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 23 '05

re: About STATIC_CHECK macro


* alex:[color=blue]
> I read famous book "Modern C++ Design". In chapter 2, there is a
> STATIC_CHECK sample:
> --------------------------------------------------
> template < bool >
> struct CompileTimeChecker
> {
> CompileTimeChecker(...);
> };
>
> template <>
> struct CompileTimeChecker< false >
> {};
>
> #define STATIC_CHECK( expr, msg )\
> {\
> class ERROR_##msg{};\
> ( void )sizeof( CompileTimeChecker< ( expr ) >( ERROR_##msg() )
> );\
> }
>
> ---------------------------------------------------------
> I use STATIC_CHECK, but get a error:
> ISO C++ forbids applying `sizeof' to a function type
>
> Why this error?[/color]

Impossible to say without seeing your code.

If you want a compile time assertion that works at file scope, try
the Boost one.

The trade-off is that the one in the book gives a more informative error
message, while the Boost one, which I as I recall uses a technique
introduced by Andrei earlier, can be used at file scope.

[color=blue]
> Why use sizeof?[/color]

The argument to sizeof is evaluated at compile time only; this is explained
in the book.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
velthuijsen@hotmail.com
Guest
 
Posts: n/a
#4: Jul 23 '05

re: About STATIC_CHECK macro


alex wrote:[color=blue]
> I read famous book "Modern C++ Design". In chapter 2, there is a
> STATIC_CHECK sample:
> --------------------------------------------------
> template < bool >
> struct CompileTimeChecker
> {
> CompileTimeChecker(...);
> };
>
> template <>
> struct CompileTimeChecker< false >
> {};
>
> #define STATIC_CHECK( expr, msg )\
> {\
> class ERROR_##msg{};\
> ( void )sizeof( CompileTimeChecker< ( expr ) >( ERROR_##msg() )
> );\
> }
>
> ---------------------------------------------------------
> I use STATIC_CHECK, but get a error:
> ISO C++ forbids applying `sizeof' to a function type
>
> Why this error? Why use sizeof?
>
> thanks![/color]

Seems you are using one of the early prints of the book (or they never
moved the online errata in the new prints)

On the website of Alexandrescu ( http://www.moderncppdesign.com/) there
is more errata for the book.

The fix is IIRC

( void )sizeof( CompileTimeChecker< ( expr ) >(( ERROR_##msg()
))

Difference is the extra pair of () around ERROR_##msg()

alex
Guest
 
Posts: n/a
#5: Jul 23 '05

re: About STATIC_CHECK macro


I see,thanks very much!

Closed Thread


Similar C / C++ bytes