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?
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[]) {
...
}
Pros and cons to everything, depending on usage:
enum class X
are disambiguated by the scope X::
int
but can be explicitly set by the programmertemplate <typename T> void f(T t) { cout << ++t; }
won\'t compile, though you can wrap an enum into a class with implicit constructor, casting operator and user-defined operators)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. Each function\'s object code could be identical (ignoring address offsets), but I wouldn\'t expect a compiler/linker to eliminate the unnecessary copies, though you could check your compiler/linker if you care.enum { A = 1, B = 2 }
- is A|B
\"legal\" from a program logic perspective?)make
and other timestamp-based recompilation tools will trigger client recompilation when they\'re changed (bad!)#define
ala #define S std::string(\"abc\")
, but the constant avoids repeated construction of distinct temporaries at each point of useconst
value, which minimises work and impact if switching between the two#define X \"x\"
and some client usage ala \"pre\" X \"post\"
, if you want or need to make X a runtime-changeable variable rather than a constant you force edits to client code (rather than just recompilation), whereas that transition is easier from a const char*
or const std::string
given they already force the user to incorporate concatenation operations (e.g. \"pre\" + X + \"post\"
for string
)sizeof
directly on a defined numeric literalunsigned
){ 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 substitution#if
statements for conditionally including code (more powerful than a post-preprocessing \"if\" as the code need not be compilable if not selected by the preprocessor), use #undef
-ine, redefine etc.make
and other timestamp-based recompilation tools will trigger client recompilation when they\'re changed (bad!)As a general rule, I use const
s and consider them the most professional option for general usage (though the others have a simplicity appealing to this old lazy programmer).
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...
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
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).
Please see here: static const vs define
usually a const declaration (notice it doesn\'t need to be static) is the way to go
Defining constants by using preprocessor directive #define
is not recommended to apply not only in C++
, but also in C
. These constants will not have the type. Even in C
was proposed to use const
for constants.
If you are defining a constant to be shared among all the instances of the class, use static const. If the constant is specific to each instance, just use const (but note that all constructors of the class must initialize this const member variable in the initialization list).
Always prefer to use the language features over some additional tools like preprocessor.
ES.31: Don\'t use macros for constants or \"functions\"
Macros are a major source of bugs. Macros don\'t obey the usual scope and type rules. Macros don\'t obey the usual rules for argument passing. Macros ensure that the human reader sees something different from what the compiler sees. Macros complicate tool building.
From C++ Core Guidelines