Re: What is the standard's scope for #define?
* Pep:
lol, this is kinda embarrassing.
It is.
I have been programming on nix using c++ for more years than I can
remember but have now hit a problem that challenges what I thought was
true. I have tried googling for the answer but cannot find a
definitive one, pardon the pun :)
So my problem is one of understanding the validity of #define pre-
processor defines across source files. I thought that if you #define
in a cpp implementation file, it will be honored in the #include
interface file. This has always worked like this for years. Now I come
to build code on windows and find that my knowledge is wrong :O
So a sample snippet to illustrate my question involves these 2 files
============================================== interface.h
#ifndef __IMPLEMENTATION__
#define __IMPLEMENTATION__
#ifndef DEFINE_VARS
extern const char externalString[];
#else
const char externalString[] = "an extern std::string";
#endif
As Victor remarked you lack an #endif.
Adding to Victor's remark about __IMPLEMENTATION__, it's Undefined Behavior.
Not yet said, as far as I can see, you should simply do
char const s[] = "blah blah";
and rely on the compiler's constant folding.
If you don't trust the compiler to do that you should not use the preprocessor,
but define the string in your implementation file.
If you don't trust the compiler to fold constants and you absolutely require a
single point of maintainance, then you can do
inline char const* s() { return "blah blah"; }
in the header.
There are also more fancy ways.
But don't do the conditional compilation thing.
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!