Pros and cons between #define
s, const
s and (what you have forgot) enum
s, depending on usage:
enum
s:
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!)const
s:
#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
s:
#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!)My personal opinion:
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).
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 anonymous 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...
#define
can lead to unexpected results:
#include <iostream>
#define x 500
#define y x + 5
int z = y * 2;
int main()
{
std::cout << "y is " << y;
std::cout << "\nz is " << z;
}
Outputs an incorrect result:
y is 505
z is 510
However, if you replace this with constants:
#include <iostream>
const int x = 500;
const int y = x + 5;
int z = y * 2;
int main()
{
std::cout << "y is " << y;
std::cout << "\nz is " << z;
}
It outputs the correct result:
y is 505
z is 1010
This is because #define
simply replaces the text. Because doing this can seriously mess up order of operations, I would recommend using a constant variable instead.
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).
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With