CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Standard Macros

  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Standard Macros

    I'm wondering if there are any "standard", and what they are ("or a link to good resources")

    For example, I've seen a ton of files that start with "#ifndef __cpluplus". I take it it implies that all c++ compilers MUST activate the __cplusplus macro.

    I'm particularly interested in knowing:
    -Debug: AFAIK, in debug build, either _DEBUG or DEBUG or always set
    -Compiler vendor/version: What are the main macros each of these compilers activate?
    -c++ version: Is there a standard macro to know if we are using C++ 98/03/0x? To know if I can/can't include r-value references, for example
    -Already included standard headers: For example, add stream functionality only if <iostream> is already included.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    Re: Standard Macros

    _DEBUG and DEBUG are not standard from what I have collected.. if I am right. NDEBUG is standard since assert must use this.

    I suppose you start with
    http://msdn.microsoft.com/en-us/library/b0084kay.aspx
    http://gcc.gnu.org/onlinedocs/cpp/Pr...defined-Macros

    or look at the standard papers. I don't have the standard, but I use the final working draft from wiki. http://www.open-std.org/jtc1/sc22/wg...2010/n3092.pdf section 16.8 Predefined macro names

    if all else, google predefined marcos for "compiler name"
    Last edited by Joeman; August 6th, 2010 at 02:39 AM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Standard Macros

    Quote Originally Posted by monarch_dodra
    I'm wondering if there are any "standard", and what they are ("or a link to good resources")

    For example, I've seen a ton of files that start with "#ifndef __cpluplus". I take it it implies that all c++ compilers MUST activate the __cplusplus macro.
    Have you considered obtaining your own copy of the C++ standard, or at least using a public draft?
    Quote Originally Posted by C++03 Section 16.8
    The following macro names shall be defined by the implementation:
    __LINE__ The line number of the current source line (a decimal constant).
    __FILE__ The presumed name of the source file (a character string literal).
    __DATE__ The date of translation of the source file (a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10). If the date of translation is not available, an implementation-defined valid date is supplied.
    __TIME__ The time of translation of the source file (a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function). If the time of translation is not available, an implementation-defined valid time is supplied.
    __STDC__ Whether __STDC__ is predefined and if so, what its value is, are implementation-defined.
    __cplusplus The name __cplusplus is defined to the value 199711L when compiling a C++ translation unit.

    The values of the predefined macros (except for __LINE__ and __FILE__) remain constant throughout the translation unit.

    If any of the pre-defined macro names in this subclause, or the identifier defined, is the subject of a #define or a #undef preprocessing directive, the behavior is undefined.
    Quote Originally Posted by monarch_dodra
    -Debug: AFAIK, in debug build, either _DEBUG or DEBUG or always set
    With respect to the assert macro from <cassert> the relevant macro is NDEBUG.

    Quote Originally Posted by monarch_dodra
    -Compiler vendor/version: What are the main macros each of these compilers activate?
    Certainly, but they are also certainly vendor specific.

    Quote Originally Posted by monarch_dodra
    -c++ version: Is there a standard macro to know if we are using C++ 98/03/0x? To know if I can/can't include r-value references, for example
    The value of __cplusplus may be relevant, though there is no difference for that between C++98 and C++03 (and C++03 is more of a "bugfix" anyway), though there might be for the next version of C++.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Standard Macros

    Well I'm not searching for strictly-standard as much as widely used and portable

    I'm writing a little library (that contains templates and cpp) that will be compiled by others. 3 of the functionalities I want to provide are:
    - Range checking in debug builds
    - Include r-value references if available (amongst others)
    - add functions if <iostream> is included

    I'm hesitating between a solution where I just ask the user to define:

    Code:
    #define MONARCH_DEBUG //for debug compilation
    #define MONARCH_USE_C++0X //for C++0X features
    or trying to "see" what the user/compiler has already defined.

    <cassert> works using the NDEBUG macro, but does that mean NDEBUG is defined in a debug build?

    for C++0x, I think the only solution is to a manual macro.

    Finally, how can I tell if <iostream> is included?

    BTW, I do have the draft, but it's still isn't my first reflex. I guess I'm a bit lazy. I'll look for vendor specific stuff myself, but I'd still like your opinion on how to do the rest.

    Thanks a lot for your answers so far anyways.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Standard Macros

    Quote Originally Posted by monarch_dodra
    <cassert> works using the NDEBUG macro, but does that mean NDEBUG is defined in a debug build?
    No, NDEBUG is supposed to be defined when it is not a debug build.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Standard Macros

    Quote Originally Posted by laserlight View Post
    No, NDEBUG is supposed to be defined when it is not a debug build.
    Oops. Well in all fairness, the only thing the standard has to say about NDEBUG is that "the effect of including either
    <cassert> or <assert.h> depends each time on the lexically current definition of NDEBUG."
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Standard Macros

    Quote Originally Posted by monarch_dodra View Post
    I'm hesitating between a solution where I just ask the user to define:

    Code:
    #define MONARCH_DEBUG //for debug compilation
    #define MONARCH_USE_C++0X //for C++0X features
    or trying to "see" what the user/compiler has already defined.
    IMO a combination of both would work best. Use your own macros, so as not to interfere with commonly used things, but at the same time provide a "config.h" file that defines good default settings for a specific compiler vendor. That way advanced users are able to get the most out of your library, but less advanced users can get it to work easily.
    for C++0x, I think the only solution is to a manual macro.
    Yes, and I think it is also the desired solution, because not every compiler supports C++0x to the same extent.
    Finally, how can I tell if <iostream> is included?
    Include it yourself. If you want to leave the option of disabling the functionality, then define a custom macro in the "config.h" file.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    Re: Standard Macros

    Quote Originally Posted by monarch_dodra View Post
    I'm writing a little library (that contains templates and cpp) that will be compiled by others. 3 of the functionalities I want to provide are:
    - Range checking in debug builds
    - Include r-value references if available (amongst others)
    - add functions if <iostream> is included
    I'd really just save yourself the trouble and instead have the user define macros indicating that those features should be used.

  9. #9
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Standard Macros

    What I'm doing for one of my more popular libraries which I'm currently rewriting is have a header file called Options.h, which has all of the macro options that the library supports, that way the end compiler just has to comment out or uncomment the options that they want.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Standard Macros

    Thankyou everyone for you insight, it helps.

    Quote Originally Posted by ninja9578 View Post
    What I'm doing for one of my more popular libraries which I'm currently rewriting is have a header file called Options.h, which has all of the macro options that the library supports, that way the end compiler just has to comment out or uncomment the options that they want.
    That was one of the options I was going for actually. I think that's what I'll do.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured