473,372 Members | 1,005 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,372 software developers and data experts.

syntax of stringizing macro

#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__,
__LINE__, #x); }

MSVC7 gives an error: "error C2014: preprocessor command must start as first
nonwhite space".

Jul 22 '05 #1
8 5314
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__,
__LINE__, #x); }

MSVC7 gives an error: "error C2014: preprocessor command must start as
first
nonwhite space".


If you want to span a macro over several lines of code, you _have_ to end
all
but the last with a \:

#define EXPECT_ASSERT(x) \
{ \
if (!x) \
expect_assert(localVariable, \
__FILE__, \
__LINE__, \
#x \
); \
}
V
Jul 22 '05 #2
Siemel Naran wrote:

#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__,
__LINE__, #x); }

MSVC7 gives an error: "error C2014: preprocessor command must start as first
nonwhite space".


Add a backslash at the end of the first line.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #3
Pete Becker <pe********@acm.org> wrote in message
Siemel Naran wrote:

#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__,
__LINE__, #x); }

MSVC7 gives an error: "error C2014: preprocessor command must start as first
nonwhite space".


Add a backslash at the end of the first line.


Thanks. I guess by end of the first line you mean before the #x
(because different browsers/newsreaders page wrap differently). The
code below is two lines: line 1 starts with "#define" and ends with
"__LINE__, \", and line 2 starts with #x.

#define EXPECT_ASSERT(x) { if (!(x)) expect_assert(outputArgs,
__FILE__, __LINE__, \
#x); }

Anyway, is this behavior standard or Microsoft specific?
Jul 22 '05 #4
"Victor Bazarov" <v.********@comAcast.net>
"Siemel Naran" <Si*********@REMOVE.att.net> wrote... #define EXPECT_ASSERT(x) \
{ \
if (!x) \
expect_assert(localVariable, \
__FILE__, \
__LINE__, \
#x \
); \
}


Thanks. This works, in addition to the suggestion Pete gave. Out of
curiosity, if have a macro

#define SOME_MACRO myfunction(__LINE__)

and we rewrite it as

#define SOME_MACRO myfunction( \
__LINE__)

will the line number passed to myfunction be the same in the following
program:

int main() { // line 10
SOME_MACRO; // will this call myfunction(10) or myfunction(11)?
}

Thanks.
Jul 22 '05 #5
Siemel Naran wrote:
"Victor Bazarov" <v.********@comAcast.net>
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...


#define EXPECT_ASSERT(x) \
{ \
if (!x) \
expect_assert(localVariable, \
__FILE__, \
__LINE__, \
#x \
); \
}

Thanks. This works, in addition to the suggestion Pete gave. Out of
curiosity, if have a macro

#define SOME_MACRO myfunction(__LINE__)

and we rewrite it as

#define SOME_MACRO myfunction( \
__LINE__)

will the line number passed to myfunction be the same in the following
program:

int main() { // line 10
SOME_MACRO; // will this call myfunction(10) or myfunction(11)?
}


Why don't you just try it? :-)

Well, concatenation of lines with the \ between them happens _before_ any
macro substitution. Since __LINE__ is a macro, the substitution has to
happen _after_ the concatenation, so the 'SOME_MACRO' example you gave
should be considered as a single line. That's basically how you can make
your macro register where it was placed in the code. Of course it does
*not* prevent you from doing

SOME_MACRO; SOME_MACRO;

and have two calls with the same value at run time, since both macros get
the same __LINE__ substitution.

V
Jul 22 '05 #6
Siemel Naran wrote:
Pete Becker <pe********@acm.org> wrote in message
Siemel Naran wrote:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__,
__LINE__, #x); }

MSVC7 gives an error: "error C2014: preprocessor command must start as first
nonwhite space".


Add a backslash at the end of the first line.

Thanks. I guess by end of the first line you mean before the #x
(because different browsers/newsreaders page wrap differently). The
code below is two lines: line 1 starts with "#define" and ends with
"__LINE__, \", and line 2 starts with #x.

#define EXPECT_ASSERT(x) { if (!(x)) expect_assert(outputArgs,
__FILE__, __LINE__, \
#x); }

Anyway, is this behavior standard or Microsoft specific?


The Standard requires that if you want something after # to be interpreted
as a preprocessor directive, there has to be nothing between the beginning
of the line and the #, except whitespace. IOW, on any _separate_ line of
code, if the preprocessor sees only whitespace followed by #, it tries to
interpret what follows the # as the directive.

V
Jul 22 '05 #7
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<3w8jd.372421
#define EXPECT_ASSERT(x) \
{ \
if (!x) \


Also, we have to use parenthesis around x, as it might be a sentence,
not a simple variable.

if (!(x)) \
Jul 22 '05 #8
na*******@excite.com (Siemel Naran) wrote:

MSVC7 gives an error: "error C2014: preprocessor command
must start as first nonwhite space".

The code below is two lines: line 1 starts with "#define" and
ends with > "__LINE__, \", and line 2 starts with #x.

#define EXPECT_ASSERT(x) { if (!(x)) expect_assert(outputArgs,
__FILE__, __LINE__, \
#x); }

Anyway, is this behavior standard or Microsoft specific?


This has got to be a compiler bug. I guess you will get the
same result if you write:

#define foo \
#x)

FWIW do you get an error if you go:

#define foo \
#error Error!
Jul 22 '05 #9

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
2
by: Steven T. Hatton | last post by:
I'm serious about this folks! I really want to see the CPP obviated. People who have never used tools such as JBuilder may not fully appreciate the advantages they provide. This isn't about...
26
by: GS | last post by:
I am doing my first real embedded programming project and the supplied device libraries have a function definition that looks like this: void FCN(void) = { INTDEF, INTABS, (unsigned short)...
9
by: Tin Gherdanarra | last post by:
Hallo, I'm trying to install pypgsql. However, I get syntax errors while compiling the C sources. The following excerpt from pgconnection.h looks a little funny to me: typedef struct {...
14
by: Henry Townsend | last post by:
Consider the little program below. It simply attempts to determine what version of what compiler it was built with via documented preprocessor macros and print out the result. When the compiler is...
1
by: Ravi | last post by:
Hi, I have this #define KB 1 #define KB_AM 33 #define KB_RM 44 #define AM 2 #define RM 3
16
by: Chuck | last post by:
Please help me correct the statements in sub "BoundData" The following sub is in a module. Everything runs with no error messages, however,data is not reaching the text box. Data is being...
2
by: babakandme | last post by:
Hi everybody:D I've a string that contains the name of a class. Some members told that I can use """Stringizing Operator (#)""", but the problem is here, that I have the string, & I want...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.