Are there any benefits of using constexpr over #define? [duplicate] - c++

This question already has answers here:
Why are preprocessor macros evil and what are the alternatives?
(8 answers)
Is constexpr really needed?
(6 answers)
Closed 7 years ago.
You definetely know that macros are somewhat evil. With the relatively new keyword constexpr we can do a few good stuff we couldn't with const. E.g:
constexpr int GetVal()
{
return 40;
}
int arr[GetVal()];
And yeah...there are also many other usages for constexpr, like in constructors..etc.
Now the question is, is there any benefit of using it over #define macros?
edit
I know what can and what cannot be done with constexpr over macros which is what most of that question and its answers are all about. Here I'm explicitly asking what are the benefits of using constexpr over #define when BOTH can be used.

Related

how to get type of auto variable in C++ [duplicate]

This question already has answers here:
Define variable b of the same type as variable a
(4 answers)
Closed 4 years ago.
I want gets the type of the auto variable and forces another variable become this type,i don't know whether c++ has such feature or function.
Use decltype.
auto a = 42;
decltype(a) b;

Benefit of constexpr on non-changing expressions [duplicate]

This question already has answers here:
When should you use constexpr capability in C++11?
(15 answers)
Closed 6 years ago.
So I understand that the use of constexpr in C++ is for defined expressions that are be evaluated at compile time, and more obviously to declare a variable or function as a constant expression. My confusion comes from understanding any benefits of using it for simple functions that will not change.
Suppose you have a function to simply square a value...
int square(int x) {
return x * x;
}
Typically that will never change, or be overridden, however, I've seen people say that it would be better practice to instead define it as...
constexpr int square(int x) {
return x * x;
}
To me, this seems like such a trivial change. Can anyone enlighten me on serious advantages of declaring such simple expressions as constexpr?
The advantage of that change is that whenever x is known at compile time, result of square could be used to initialize constexpr variables or as a template argument.

Is there a big reason/difference between using char *argv[] instead of char* argv[]? [duplicate]

This question already has answers here:
C - initialization of pointers, asterisk position [duplicate]
(9 answers)
Closed 8 years ago.
It might seems a noob question but I simply didnt find anywhere, and I'm curious about as a starter in C/C++. In what side I should put the asterisk? I've seen people using both declarations.
Nothing, though I prefer the latter version as I read * as "pointer".
So T* x; means x is a pointer to T.
Note that T* x, y; is the same as:
T* x;
T y;
Which is perhaps why some people recommend placing the * next to the variable name, but I personally just don't do multiple declarations on the same line because I think it's a bit ugly.

What does this mean in C language? [duplicate]

This question already has answers here:
What is the meaning of prepended double colon "::"?
(9 answers)
Closed 9 years ago.
What does this do in C language and please explain this with simplified example ?
void KidLogic::doCommand()
{ }
In C, it doesn't mean anything.
This is C++, and it means the KidLogic namespace or class has a doCommand function doing nothing.
It looks like C++, not C.
It is a definition of the method doCommand for class (or namespace) KidLogic.

Please explain constexpr [duplicate]

This question already has answers here:
When should you use constexpr capability in C++11?
(15 answers)
Closed 9 years ago.
I have a class which contains three static constants,
static const int NUM_POINTS = 2000;
static const float LAKE_THRESHOLD = 0.3;
static const int NUM_LLOYD_ITERATIONS = 2;
In the header file. I realize that now in C++11 I have to use a constexpr but I can't figure out how to use them. Can anyone explain constexpr in a simple way?
constexpr can be used to mark an expression as compile-time constant. It extends to functions as well, so an arbitrarily deep call chain can be compile-time constant. This permits the compiler to substitute the constant value, rather than evaluating it unnecessarily at runtime.
See: http://en.cppreference.com/w/cpp/language/constexpr