About STATIC_CHECK macro

A

alex

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!
 
A

Alf P. Steinbach

* alex:
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?

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.

Why use sizeof?

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

velthuijsen

alex said:
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!

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top