Could anybody explain this macro for me?

W

webinfinite

#define D(y...) (const int []) {y}

My understand is that D is taking in a various length parameter y
which is an array of const int. Am I right?

Thanks.
 
O

Old Wolf

#define D(y...) (const int []) {y}

My understand is that D is taking in a various length parameter y
which is an array of const int. Am I right?

This is a syntax error. Are you sure you transcribed
it correctly?
 
J

Juha Nieminen

Old said:
#define D(y...) (const int []) {y}

My understand is that D is taking in a various length parameter y
which is an array of const int. Am I right?

This is a syntax error. Are you sure you transcribed
it correctly?

To me it looks like a variadic macro from the newer C standard, not
yet in the C++ standard.
 
I

Ian Collins

#define D(y...) (const int []) {y}

My understand is that D is taking in a various length parameter y
which is an array of const int. Am I right?
Try c.l.c, this looks like C99.
 
M

Michael DOUBEZ

Tim H a écrit :
#define D(y...) (const int []) {y}
My understand is that D is taking in a various length parameter y
which is an array of const int. Am I right?
Try c.l.c, this looks like C99.
That's not any C99 syntax I know of, either. C99 provides __VA_ARG__
for variadic macros.

This is a gnu cpp extension that allows you to write variadic macros
that way:

#define foo(args...) bar(arg1,arg2,args)
 
J

James Kanze

Old said:
#define D(y...) (const int []) {y}
My understand is that D is taking in a various length
parameter y which is an array of const int. Am I right?
This is a syntax error. Are you sure you transcribed it
correctly?
To me it looks like a variadic macro from the newer C
standard, not yet in the C++ standard.

There are actually two things in the above which cause syntax
errors in current C++. The first is that it is a variadic
macro, which is legal in C99 and will be legal in the next
version of C++. The second is that when expanded, this results
in a compound literal. Again, C99, but not current C++; in this
case, I know that the next version will extend the
initialization syntax, but I'm not sure whether the extension
will look exactly like a compound literal in C or not.
 
O

Old Wolf

Old said:
On Nov 13, 12:46 pm, "(e-mail address removed)" <[email protected]>

#define D(y...) (const int []) {y}

There are actually two things in the above which cause syntax
errors in current C++.  The first is that it is a variadic
macro, which is legal in C99 and will be legal in the next
version of C++.  

Variadic macros in C99 look like:

#define D(y, ...)

i.e. the ellipsis is separated from the other
arguments by a comma (with optional whitespace).

The ellipsis directly following the argument
name, isn't in any C standard. (Another poster
suggests that it's a GCC extension).
 
J

James Kanze

Old Wolf wrote:
On Nov 13, 12:46 pm, "(e-mail address removed)" <[email protected]>
#define D(y...) (const int []) {y}
There are actually two things in the above which cause syntax
errors in current C++.  The first is that it is a variadic
macro, which is legal in C99 and will be legal in the next
version of C++.  
Variadic macros in C99 look like:
  #define D(y, ...)
i.e. the ellipsis is separated from the other
arguments by a comma (with optional whitespace).

So I see. I wonder why they didn't support the form without the
comma, as they do in function declarations. Shocking lack of
orthogonality.

(Historically, the comma wasn't allowed in function
declarations; the syntax was exactly that of a natural language,
where you would never write "a, b, ..." but always "a, b...".
When C adopted this syntax from C++, the C committee decided to
allow the syntax with the comma as well; I'm not sure what their
reasoning was, but I also find it more appealing, separating the
.... from the preceding argument. But not enough more appealing
to have justified changing or extending the existing
specification; if it were a new invention, I'd define it with
the comma, but since it wasn't, and isn't, I don't think that
there was, or is, sufficient justification to add the form with
the comma.)
The ellipsis directly following the argument name, isn't in
any C standard. (Another poster suggests that it's a GCC
extension).

Might be. I would certainly expect that most implementations
supporting vararg templates support it (except maybe in their
strictest modes), much like they support a final comma in an
enum list. There is absolutely no reason not to be orthogonal
here (and I would consider this a defect in the C standard).
 
O

Old Wolf

Might be.  I would certainly expect that most implementations
supporting vararg templates support it (except maybe in their
strictest modes), much like they support a final comma in an
enum list.  There is absolutely no reason not to be orthogonal
here (and I would consider this a defect in the C standard).

Well, the gcc-3.4.4 I have here, running in
standard mode, just gives 'parse error' for:

int foo(int x...) { }

I've never encountered this form (without the
comma), and I usually do read the list of
extensions in compiler documentation when using
a new compiler. Obviously you have more experience
with compilers than I do though!
 
J

James Kanze

Well, the gcc-3.4.4 I have here, running in standard mode,
just gives 'parse error' for:
  int foo(int x...) { }

All I get for it in 4.2.1 (the only version I have available
here) is:
varargs.cc:8: warning: control reaches end of non-void function
That's compiled with
g++ -std=c++98 -pedantic -ffor-scope -fno-gnu-keywords -foperator-
names -pipe -Wall -W -Wno-sign-compare -Wno-deprecated -Wno-non-
virtual-dtor -Wpointer-arith -Wno-unused -Wno-switch -Wno-missing-
braces -Wno-long-long -static-libgcc -ggdb3 -D_GLIBCXX_CONCEPT_CHECKS -
D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC
(my standard options).
I've never encountered this form (without the comma), and I
usually do read the list of extensions in compiler
documentation when using a new compiler. Obviously you have
more experience with compilers than I do though!

Not so much compilers. According to the first edition of _The
C++ Programming Language__ (1986), the comma isn't legal.
According to the C90 or the C++98 standard, it is optional.
I've never seen a compiler that didn't accept it either way, but
then, I don't think I'd have noticed if it didn't; I can't
remember ever actually using varargs in C++.

Since both are required by the standard, I wouldn't expect to
find support for both mentionned in the compiler documentation.
 
O

Old Wolf

All I get for it in 4.2.1 (the only version I have available
here) is:
    varargs.cc:8: warning: control reaches end of non-void function
That's compiled with
    g++ -std=c++98 -pedantic -ffor-scope -fno-gnu-keywords -foperator-
names -pipe -Wall -W -Wno-sign-compare -Wno-deprecated -Wno-non-
virtual-dtor -Wpointer-arith -Wno-unused -Wno-switch -Wno-missing-
braces -Wno-long-long -static-libgcc -ggdb3 -D_GLIBCXX_CONCEPT_CHECKS -
D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC
(my standard options).


Not so much compilers.  According to the first edition of _The
C++ Programming Language__ (1986), the comma isn't legal.
According to the C90 or the C++98 standard, it is optional.
I've never seen a compiler that didn't accept it either way, but
then, I don't think I'd have noticed if it didn't; I can't
remember ever actually using varargs in C++.

Since both are required by the standard, I wouldn't expect to
find support for both mentionned in the compiler documentation.

I tried on another machine with gcc 4.0.1 .
g++ allows int foo(int x...) , gcc gives 'syntax error'.

I checked in C99 again and the grammar specification
doesn't seem to permit int foo(int x...) , the
relevant bits are:

direct-declarator:
direct-declarator ( parameter-type-list )
[snip]

parameter-type-list:
parameter-list
parameter-list , ...

Are you sure C90 allowed it?
 
J

James Kanze

I tried on another machine with gcc 4.0.1 .
g++ allows int foo(int x...) , gcc gives 'syntax error'.
I checked in C99 again and the grammar specification
doesn't seem to permit int foo(int x...) , the
relevant bits are:
direct-declarator:
direct-declarator ( parameter-type-list )
[snip]
parameter-type-list:
parameter-list
parameter-list , ...
Are you sure C90 allowed it?

I'll check tonight (my only copy of C90 is at home), if I
remember it, but if C99 doesn't allow it, it's highly unlikely
that C90 did; the C committee is even stricter about not
breaking existing code that is the C++ committee. (And I was
sure that C99 also allowed it, but I can't find what made me
think that in the standard.)
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top