473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

About STATIC_CHECK macro

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!

Jul 23 '05 #1
4 2300
alex wrote:
I read famous book "Modern C++ Design". In chapter 2, there is a
STATIC_CHECK sample:


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

Jul 23 '05 #2
* 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.

--
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?
Jul 23 '05 #3
alex wrote:
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()

Jul 23 '05 #4
I see,thanks very much!

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
2
by: Rex_chaos | last post by:
I have define a MACRO varible as a common variable that be refered by several classes. #ifndef COMMON_VAR #define COMMON_VAR 1 .... #endif
1
by: LRS Kumar | last post by:
In Chapter 1(Page 25 in my copy) of Modern C++ Design, Alexandrescu uses a macro called STATIC_CHECK: #define STATIC_CHECK(expr, msg) \ {\ class ERROR_##msg {};...
36
by: Peng Jian | last post by:
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) I'm a beginner learning C. I can't see what *(a) == *(b) is for. If either a or b is a null pointer, this may cause crash. And if...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
11
by: Remo | last post by:
Hi All I Have Quation About Macro's plz help why do we have # symbol before any macro line #define STOP 0 ... why con't we use the any other symbol like $, @,, plz answer if any one knows...
6
by: Michael B Allen | last post by:
Hi, I have a macro that looks like the following: #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0 which if used in some code like: MMSG("foo=%s", foo);
3
by: prix prad | last post by:
Hi All, I encountered the above error when I tried to expand a macro as follows: #define EXPAND(array) int M_ ## array The problem occurs when we have 'array' itself as a macro: say...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.