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

meaning of define

What is meaning of the following define:-

#define x(argl...) x1(##argl)

Aug 31 '06 #1
17 2748
ni**********@gmail.com wrote:
What is meaning of the following define:-

#define x(argl...) x1(##argl)
I don't think variadic macros are allowed in C++, so the ... part is
not standard. The ## is the token pasting operator. It pastes the token
that you pass as an argument. For example the macro

#define var(n) var##n

would cause 'var(3)' to be expanded to 'var3'.

Regards,
Bart.

Aug 31 '06 #2

Bart wrote:
ni**********@gmail.com wrote:
What is meaning of the following define:-

#define x(argl...) x1(##argl)

I don't think variadic macros are allowed in C++, so the ... part is
not standard. The ## is the token pasting operator. It pastes the token
that you pass as an argument. For example the macro

#define var(n) var##n

would cause 'var(3)' to be expanded to 'var3'.

Regards,
Bart.
I have tried this and this compiles on c++ compiler but if you try
something like
#define x(argl,...) x1(##arl)
or
#define x(...) x1(##)
compiler throws error. So can you explain how this is being expanded? I
think normal syntax of variable number of argument is foo(some arg,...)
or simply foo(...).
Thanks,
Niraj

Aug 31 '06 #3

## operator in C++ is used inside macros to concatenate two tokens for
example :-

#include <iostream>
using namespace std;
#define func(x) x##out<<"Hello!"<<endl

int main(){
func(c);
system("pause");
}

Above programme will print Hello! on screen.
I have tried this and this compiles on c++ compiler but if you try
something like
#define x(argl,...) x1(##arl)
or
#define x(...) x1(##)
compiler throws error. So can you explain how this is being expanded? I
think normal syntax of variable number of argument is foo(some arg,...)
or simply foo(...).
Thanks,
Niraj

Aug 31 '06 #4
flamexx7 posted:
## operator in C++ is used inside macros to concatenate two tokens

It's nothing to do with the C++ language itself -- it's purely to do with the
preprocessor.

--

Frederick Gotham
Aug 31 '06 #5
ni**********@gmail.com wrote:
<snip>
I have tried this and this compiles on c++ compiler
That your compiler has accepted it doesn't mean that it's standard C++.
IIRC the C99 standard allows variadic macros, and some compilers have
accepted them prior to that, but that doesn't make it legal C++.

<snip>
I think normal syntax of variable number of argument is foo(some arg,...)
or simply foo(...).
No, the second foo is not valid. Functions with a variable number of
arguments must have at least one formal argument for the va_start macro
to work. Again, some compilers have extensions that allow foo(...), but
that's not standard C++.

Regards,
Bart.

Aug 31 '06 #6
Frederick Gotham wrote:
flamexx7 posted:
## operator in C++ is used inside macros to concatenate two tokens

It's nothing to do with the C++ language itself -- it's purely to do with the
preprocessor.
Well, the standard which defines the C++ language also defines the
preprocessor, so from a purely formal point of view the preprocessor IS
part of the language. The reasons for which people think of the
preprocessor as a separate entity from the language are mostly
historical and practical.

Regards,
Bart.

Aug 31 '06 #7
flamexx7 wrote:
>
## operator in C++ is used inside macros to concatenate two tokens


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>


Brian
Aug 31 '06 #8
Frederick Gotham wrote:
flamexx7 posted:
>## operator in C++ is used inside macros to concatenate two tokens


It's nothing to do with the C++ language itself -- it's purely to do with
the preprocessor.
The preprocessor is part of the C++ language.

Sep 1 '06 #9
Rolf Magnus posted:
>It's nothing to do with the C++ language itself -- it's purely to do with
the preprocessor.

The preprocessor is part of the C++ language.

Yes, but the _actual_ C++ compiler doesn't know anything about #define... all
of that has been stripped away in the preprocessor phase.

--

Frederick Gotham
Sep 1 '06 #10
"Frederick Gotham" wrote:
Rolf Magnus posted:
>>It's nothing to do with the C++ language itself -- it's purely to do
with
the preprocessor.

The preprocessor is part of the C++ language.


Yes, but the _actual_ C++ compiler doesn't know anything about #define...
all
of that has been stripped away in the preprocessor phase.

--

Frederick Gotham
Couldn't you just admit that you shouldn't have made your initial post and
let it go at that? It is not necessary that you respond to each and every
message. AFAIK the preprocessor is a *conceptual* phase. Conceptual means
"not real".
Sep 1 '06 #11
osmium posted:
Couldn't you just admit that you shouldn't have made your initial post
and let it go at that?

No, I believe my initial post my appropriate. Just today we had someone
asking why their "#define" struct wouldn't compile properly.

--

Frederick Gotham
Sep 1 '06 #12
Frederick Gotham wrote:
Rolf Magnus posted:
It's nothing to do with the C++ language itself -- it's purely to do with
the preprocessor.
The preprocessor is part of the C++ language.

Yes, but the _actual_ C++ compiler doesn't know anything about #define... all
of that has been stripped away in the preprocessor phase.
The C++ standard doesn't say how the actual C++ compiler has to be
structured. It only talks about requirements on implementation. The
implementation is only required to behave _as if_ all phases of
translation occur, but whether the phases physically occur in separate
steps is not specified.

Actually, preprocessing is itself divided into two translation phases,
and several other phases which are not usually thought of as such also
occur (e.g. mapping source characters and folding line continuations
are two separate phases). Like I said previously, preprocessing is
viewed as a separate part of the translation process for historical
reasons and because it is practical to think of it as such.

Regards,
Bart.

Sep 1 '06 #13
In article <4l************@individual.net>, r1********@comcast.net
says...

[ ... ]
Couldn't you just admit that you shouldn't have made your initial post and
let it go at that? It is not necessary that you respond to each and every
message. AFAIK the preprocessor is a *conceptual* phase. Conceptual means
"not real".
Not really. The phases of translation are entirely real, and later
phases really can't take actions based on the source code as it existed
before a previous phase of translation did its thing.

It is true that there's no requirement that the phases of translation
necessarily produce successive versions of the source code, nor that the
version of the source code that would be the output of a given phase
necessarily exist at any point. Nonetheless, in the compilers I've
looked at, the preprocessor and compiler are distinct to at least some
degree. In some cases, they're entirely separate programs, while in
others they're compiled into a single program, but still operate
separately from each other.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 1 '06 #14

Jerry Coffin wrote:
<snip>
It is true that there's no requirement that the phases of translation
necessarily produce successive versions of the source code, nor that the
version of the source code that would be the output of a given phase
necessarily exist at any point. Nonetheless, in the compilers I've
looked at, the preprocessor and compiler are distinct to at least some
degree. In some cases, they're entirely separate programs, while in
others they're compiled into a single program, but still operate
separately from each other.
But the same could be said of other parts of the compiler as well.

Regards,
Bart.

Sep 1 '06 #15
While we're on the subject... I do have a question or two regarding
#defines...

1) Does the text-substitution occur across the whole file, or from the
#define onwards?

2) How / in what order does it do the substitutions? In that if I have:

#define DEF1 DEF2
#define DEF2 DEF3
#define DEF3 doStuff()

Would it do a pass for each #define, performing the substitution for
all occurences of DEF# in the file (including those in the other
#defines), remove the #define statement, then move onto the next define
statement? Or does it just do a single pass for the whole file,
performing text substitutions for the defined words only in the actual
c++ code (i.e. not performing text-substitutions for occurences within
another define statement)?

Cheers,

Siam

Sep 1 '06 #16
In article <11**********************@74g2000cwt.googlegroups. com>,
ba***********@gmail.com says...
>
Jerry Coffin wrote:
<snip>
It is true that there's no requirement that the phases of translation
necessarily produce successive versions of the source code, nor that the
version of the source code that would be the output of a given phase
necessarily exist at any point. Nonetheless, in the compilers I've
looked at, the preprocessor and compiler are distinct to at least some
degree. In some cases, they're entirely separate programs, while in
others they're compiled into a single program, but still operate
separately from each other.

But the same could be said of other parts of the compiler as well.
Of course -- but this makes it no less true of the compiler vs.
preprocessor.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 1 '06 #17
In article <11**********************@i3g2000cwc.googlegroups. com>,
si*****@gmail.com says...
While we're on the subject... I do have a question or two regarding
#defines...

1) Does the text-substitution occur across the whole file, or from the
#define onwards?
From the #define onwards. You can even define the same token as having
different substitutions at different points in the same file, if you
want to -- for example:

#define X Y
// ...
#undef X
#define X Z
// ...
2) How / in what order does it do the substitutions? In that if I have:

#define DEF1 DEF2
#define DEF2 DEF3
#define DEF3 doStuff()

Would it do a pass for each #define, performing the substitution for
all occurences of DEF# in the file (including those in the other
#defines), remove the #define statement, then move onto the next define
statement? Or does it just do a single pass for the whole file,
performing text substitutions for the defined words only in the actual
c++ code (i.e. not performing text-substitutions for occurences within
another define statement)?
After all parameters in the replacement list have been substituted, the
resulting preprocessing token sequence is rescanned with all subsequent
preprocessing tokens of the source file for more macro names to replace.

If the name of the macro being replaced is found during this scan of the
replacement list (not including the rest of the source file?s
preprocessing tokens), it is not replaced.

(Not my words -- that's section 16.3.4/1 and 16.3.4/2 of the standard.)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 2 '06 #18

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

Similar topics

16
by: godfather2 | last post by:
this should be an easy one ... what is the meaning of "1u", for instance: #define DUMMY (1U<<31) as opposed to just: #define DUMMY (1<<31)
6
by: Muralidhar | last post by:
Hi all, I saw in one function that its return type was written as PACKED...I wasnt sure what that would mean. Any pointers on this? Also, what is the significance of specifically mentioning...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
9
by: Qiao Jian | last post by:
I am new to c. Today I just read an h file within which there is statements: #ifndef _RANDOM_H #define _RANDOM_H So what is the meaning or purpose of this statement? When should I use such...
1
by: DaVinci | last post by:
I had thinked about the implements of variable argument for a long time. But I only know a little about it on the following macro. Could you please explain it for me? more detail.Thanks very...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
4
by: _mario.lat | last post by:
for struct: struct in6_addr { uint8_t s6_addr; }; is provided a costant: #define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}} what does means {{{, and }}}?
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
4
by: StdNewer | last post by:
#ifndef OF /* function prototypes */ # ifdef STDC # define OF(args) args # else # define OF(args) () # endif #endif void func OF( args );
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.