[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Could anybody explain this macro for me?

webinfinite@gmail.com

11/12/2008 11:46:00 PM

#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.
13 Answers

Old Wolf

11/13/2008 12:16:00 AM

0

On Nov 13, 12:46 pm, "webinfin...@gmail.com" <webinfin...@gmail.com>
wrote:
> #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?


Juha Nieminen

11/13/2008 12:28:00 AM

0

Old Wolf wrote:
> On Nov 13, 12:46 pm, "webinfin...@gmail.com" <webinfin...@gmail.com>
> wrote:
>> #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.

Ian Collins

11/13/2008 3:07:00 AM

0

webinfinite@gmail.com wrote:
> #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.

--
Ian Collins

Tim H

11/13/2008 4:44:00 AM

0

On Nov 12, 7:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> webinfin...@gmail.com wrote:
> > #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.
>
> --
> Ian Collins

That's not any C99 syntax I know of, either. C99 provides __VA_ARG__
for variadic macros.

Michael DOUBEZ

11/13/2008 8:28:00 AM

0

Tim H a écrit :
> On Nov 12, 7:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> webinfin...@gmail.com wrote:
>>> #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)

--
Michael

James Kanze

11/13/2008 10:04:00 AM

0

On Nov 13, 1:28 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> Old Wolf wrote:
> > On Nov 13, 12:46 pm, "webinfin...@gmail.com" <webinfin...@gmail.com>
> > wrote:
> >> #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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Old Wolf

11/15/2008 4:05:00 AM

0

On Nov 13, 11:04 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 13, 1:28 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> > Old Wolf wrote:
> > > On Nov 13, 12:46 pm, "webinfin...@gmail.com" <webinfin...@gmail.com>
> > >
> > >> #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).

James Kanze

11/15/2008 10:00:00 AM

0

On Nov 15, 5:04 am, Old Wolf <oldw...@inspire.net.nz> wrote:
> On Nov 13, 11:04 pm, James Kanze <james.ka...@gmail.com> wrote:

> > On Nov 13, 1:28 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> > > Old Wolf wrote:
> > > > On Nov 13, 12:46 pm, "webinfin...@gmail.com" <webinfin...@gmail.com>

> > > >> #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).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Juha Nieminen

11/15/2008 5:08:00 PM

0

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

Straight from the horse's mouth:

http://gcc.gnu.org/onlinedocs/cpp/Variadic-M...

Old Wolf

11/15/2008 10:21:00 PM

0

On Nov 15, 11:00 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 15, 5:04 am, Old Wolf <oldw...@inspire.net.nz> wrote:
> > 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).

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!