Could anybody explain this macro for me?

Discussion in 'C++' started by webinfinite, Nov 12, 2008.

  1. webinfinite

    webinfinite Guest

    #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?

    webinfinite, Nov 12, 2008
    1. Advertisements

  2. webinfinite

    Old Wolf Guest

    This is a syntax error. Are you sure you transcribed
    it correctly?
    Old Wolf, Nov 13, 2008
    1. Advertisements

  3. To me it looks like a variadic macro from the newer C standard, not
    yet in the C++ standard.
    Juha Nieminen, Nov 13, 2008
  4. webinfinite

    Ian Collins Guest

    Try c.l.c, this looks like C99.
    Ian Collins, Nov 13, 2008
  5. webinfinite

    Tim H Guest

    That's not any C99 syntax I know of, either. C99 provides __VA_ARG__
    for variadic macros.
    Tim H, Nov 13, 2008
  6. Tim H a écrit :
    This is a gnu cpp extension that allows you to write variadic macros
    that way:

    #define foo(args...) bar(arg1,arg2,args)
    Michael DOUBEZ, Nov 13, 2008
  7. webinfinite

    James Kanze Guest

    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, Nov 13, 2008
  8. webinfinite

    Old Wolf Guest

    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).
    Old Wolf, Nov 15, 2008
  9. webinfinite

    James Kanze Guest

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

    (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.)
    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, Nov 15, 2008
  10. Juha Nieminen, Nov 15, 2008
  11. webinfinite

    Old Wolf Guest

    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!
    Old Wolf, Nov 15, 2008
  12. webinfinite

    James Kanze Guest

    All I get for it in 4.2.1 (the only version I have available
    here) is: 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 -
    (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.
    James Kanze, Nov 15, 2008
  13. webinfinite

    Old Wolf Guest

    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 ( parameter-type-list )

    parameter-list , ...

    Are you sure C90 allowed it?
    Old Wolf, Nov 16, 2008
  14. webinfinite

    James Kanze Guest

    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.)
    James Kanze, Nov 17, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.