Verstehe Mini-Makro nicht



  • Es geht um folgendes:

    #define MDL_STR(SYM) #SYM
    

    Was macht das Ding?



  • Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the next preprocessing token in the replacement list.



  • f(MDL_STR(hallo));
    

    wird zu

    f("hallo");
    


  • Es setzt SYM in Anführungszeichen.

    MDL_STR(int) -> "int"
    

    Falsches Forum.



  • Sone schrieb:

    Falsches Forum.

    Wieso? Bietet C++ eine moderne Alternative zu #-Makros?

    Ich finde zu C++ gehoert auch C und deswegen sollte man hier schon auch mal C spezifische Fragen stellen koennen - ansonsten besteht die Gefahr, dass man im C-Forum mit irgendwelchem C-Zeug zugebombt wird anstatt eine Loesung fuer C++ zu bekommen.



  • C++ _ public C schrieb:

    ...

    Jaja, red keinen Blödsinn.
    Das ist der C99-Präprozessor, der gehört ins C-Forum. Basta. Die Frage war klar und offensichtlich, nicht so, als hätte sie potenziell etwas mit C++ am Hut.



  • Sone schrieb:

    Jaja, red keinen Blödsinn.
    Das ist der C99-Präprozessor

    self-owned



  • Hacker schrieb:

    Sone schrieb:

    Jaja, red keinen Blödsinn.
    Das ist der C99-Präprozessor

    self-owned

    N3337 16.3.2 (The # operator) schrieb:

    1. Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter
    as the next preprocessing token in the replacement list.

    2. A character string literal is a string-literal with no prefix. If, in the replacement list, a parameter is
    immediately preceded by a # preprocessing token, both are replaced by a single character string literal
    preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding
    argument. Each occurrence of white space between the argument’s preprocessing tokens becomes a single
    space character in the character string literal. White space before the first preprocessing token and after
    the last preprocessing token comprising the argument is deleted. Otherwise, the original spelling of each
    preprocessing token in the argument is retained in the character string literal, except for special handling
    for producing the spelling of string literals and character literals: a \ character is inserted before each " and
    \ character of a character literal or string literal (including the delimiting " characters). If the replacement
    that results is not a valid character string literal, the behavior is undefined. The character string literal
    corresponding to an empty argument is "". The order of evaluation of # and ## operators is unspecified.

    N1256 6.10.3.2 (The # operator) schrieb:

    1. Each # preprocessing token in the replacement list for a function-like macro shall be
    followed by a parameter as the next preprocessing token in the replacement list.
    Semantics
    2. If, in the replacement list, a parameter is immediately preceded by a # preprocessing
    token, both are replaced by a single character string literal preprocessing token that
    contains the spelling of the preprocessing token sequence for the corresponding
    argument. Each occurrence of white space between the argument’s preprocessing tokens
    becomes a single space character in the character string literal. White space before the
    first preprocessing token and after the last preprocessing token composing the argument
    is deleted. Otherwise, the original spelling of each preprocessing token in the argument
    is retained in the character string literal, except for special handling for producing the
    spelling of string literals and character constants: a \ character is inserted before each "
    and \ character of a character constant or string literal (including the delimiting "
    characters), except that it is implementation-defined whether a \ character is inserted
    before the \ character beginning a universal character name. If the replacement that
    results is not a valid character string literal, the behavior is undefined. The character
    string literal corresponding to an empty argument is "". The order of evaluation of # and
    ## operators is unspecified.

    Ich hab die gleichen Teile mal Fett gemacht.
    ...
    Ich bin sicher, der C99-Standard hat alles vom C++-Standard genommen 🙄



  • Sone/Hacker, auch wenns vom C-Präprozessor kommt, ists im C++ Präprozessor und deshalb genauso ein C++-Thema. Wenn jemand nach dem Unterschied zwischen float und double in C++ fragt schickt man ihn auch nicht ins C-Forum. (Und nein, ich werd mich jetzt nicht mit Haarspaltereien auseinandersetzen).



  • Fakt ist, dass es # schon in C90 gab und in C++ immer noch gibt. Ergo ist es nicht der C99-Präprozessor => Blödsinn.



  • Hacker schrieb:

    Fakt ist, dass es # schon in C90 gab und in C++ immer noch gibt. Ergo ist es nicht der C99-Präprozessor => Blödsinn.

    Wie du meinst. 🙂

    Die Frage des TEs wurde jetzt ja ohnehin beantwortet.


Anmelden zum Antworten