Is it better to use static const
vars than #define
preprocessor? Or maybe it depends on the context?
What are advantages/disadvantages for each method?
Is it better to use static const
vars than #define
preprocessor? Or maybe it depends on the context?
What are advantages/disadvantages for each method?
Using a static const is like using any other const variables in your code. This means you can trace wherever the information comes from, as opposed to a #define that will simply be replaced in the code in the pre-compilation process.
You might want to take a look at the C++ FAQ Lite for this question: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7
Please see here: static const vs define
usually a const declaration (notice it doesn't need to be static) is the way to go
Personally, I loathe the preprocessor, so I'd always go with const.
The main advantage to a #define is that it requires no memory to store in your program, as it is really just replacing some text with a literal value. It also has the advantage that it has no type, so it can be used for any integer value without generating warnings.
Advantages of "const"s are that they can be scoped, and they can be used in situations where a pointer to an object needs to be passed.
I don't know exactly what you are getting at with the "static" part though. If you are declaring globally, I'd put it in an anonomous namespace instead of using static. For example
namespace {
unsigned const seconds_per_minute = 60;
};
int main (int argc; char *argv[]) {
...
}
If this is a C++ question and it mentions #define
as an alternative, then it is about "global" (i.e. file-scope) constants, not about class members. When it comes to such constants in C++ static const
is redundant. In C++ const
have internal linkage by default and there's no point in declaring them static
. So it is really about const
vs. #define
.
And, finally, in C++ const
is preferable. At least because such constants are typed and scoped. There are simply no reasons to prefer #define
over const
, aside from few exceptions.
String constants, BTW, are one example of such an exception. With #define
d string constants one can use compile-time concatenation feature of C/C++ compilers, as in
#define OUT_NAME "output"
#define LOG_EXT ".log"
#define TEXT_EXT ".txt"
const char *const log_file_name = OUT_NAME LOG_EXT;
const char *const text_file_name = OUT_NAME TEXT_EXT;
P.S. Again, just in case, when someone mentions static const
as an alternative to #define
, it usually means that they are talking about C, not about C++. I wonder whether this question is tagged properly...
Usually you should prefer static consts. It has no disadvantage. The prprocessor should mainly be used for conditional compilation (and sometimes for really dirty trics maybe).
(Originally posted in response to another question that was closed for duplicating this one - reproducing my answer here incase the other closed question is purged - do let me know if this isn't an appropriate procedure)
Pros and cons to everything, depending on usage:
template <typename T> void f(T t) { cout << ++t; }
won't compile)template <typename T> void f(T)
get a distinct instantiation when passed the same numeric value from different enums, all of which are distinct from any actual f(int) instantiation.#define
ala #define S std::string("abc")
, but the constant avoids repeated construction of distinct temporaries at each point of use#define X "x"
and some client usage ala "pre" X "post"
, you're in trouble if you want or need to make X a runtime-changeable variable rather than a constant, whereas that transition is easier from a const char*
or const std::string
given they already force the user to incorporate concatenation operations.{ 1, 2 }
that can be used to initialise arrays, or #define MICROSECONDS *1E-6
etc. (definitely not recommending this!)__FILE__
and __LINE__
can be incorporated into the macro substitutionAs a general rule, I use consts and consider them the most professional option for general usage (though the others have a simplicity appealing to this old lazy programmer).