By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

syntax of stringizing macro

P: n/a
#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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.