|
-
August 6th, 2010, 02:19 AM
#1
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.
-
August 6th, 2010, 02:34 AM
#2
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
-
August 6th, 2010, 02:46 AM
#3
Re: Standard Macros
 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? 
 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.
 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.
 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.
 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++.
-
August 6th, 2010, 03:27 AM
#4
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.
-
August 6th, 2010, 03:30 AM
#5
Re: Standard Macros
 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.
-
August 6th, 2010, 04:08 AM
#6
Re: Standard Macros
 Originally Posted by laserlight
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.
-
August 6th, 2010, 05:25 AM
#7
Re: Standard Macros
 Originally Posted by monarch_dodra
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
-
August 6th, 2010, 05:26 AM
#8
Re: Standard Macros
 Originally Posted by monarch_dodra
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.
-
August 6th, 2010, 06:33 AM
#9
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.
-
August 6th, 2010, 07:51 AM
#10
Re: Standard Macros
Thankyou everyone for you insight, it helps.
 Originally Posted by ninja9578
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|