What are the differences between macros and constexpr? When is it preferable to use one instead of the other?

3

I have not had contact with C ++ for some time, until I decided to create a project to test a solution. As I knew before, I created a macro to use a value as "constant" later, eg:

#define MAX_ENTRIES = 10

So Visual Studio recommended that you replace the macro with constexpr as follows:

constexpr auto MAX_ENTRIES = 10;

As far as I know, a macro does not represent the value of a variable, it's just a piece of code that the compiler will replace every time it finds referenced in the middle of the code, right?

So I noticed, constexpr has the type of the variable. Is that one of the advantages? I'm kind of obliged to assign a type to the "variable" that I'll use as a constant.

When is it preferable to use Macro instead of constexpr (and vice versa)? And what are the main differences between these two approaches?

    
asked by anonymous 23.09.2018 / 04:26

1 answer

3
  

When is it preferable to use Macro instead of constexpr (and vice versa)? And what are the main differences between these two approaches?

Macros in C ++? Never! This is not a language feature, it's a legacy feature of C that stayed in C ++.

Unless you really have no other way, you should not use macros. And even if you do not have it maybe the case to rethink what you are doing.

There are many things that replace macros even in C existed: automatic compiler optimizations with inline , const , enum , typedef , templates , etc. A few more things that were even complicated to do with macro and still have some difficulty comes in the next versions. And some things should never be used like macros. constexpr is just one more of them.

The example used would suffice a% w / o% that has always existed in C ++. Even many already consider using inappropriate ALL_CASE outside the macro. This form was necessary to indicate that it was using something dangerous, which was not part of the language (macro is not C, it's a preprocessor thing that always comes with C).

In some cases, const is required because the compiler needs to know that something is a expression constant that can be solved at compile time (simplified) and should do so for use somewhere else. This is constexpr :

constexpr int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n - 1)); }

If constexpr receives a value that is constant and resolved at compile time it is possible to eliminate the call to this function completely, it is more than inline , it will not even be executed running, it performs during compilation and gets the result.

Templates need a lot of their use.

  

As far as I know, a macro does not represent the value of a variable, it's just a piece of code that the compiler will replace every time it finds referenced in the middle of the code, right?

Okay, it's a text replacement with no semantics, your problem will work out and it will turn out what you expect.

  

From what I've noticed, constexpr has the type of the variable. Is that one of the advantages? I'm kind of obliged to assign a type to the "variable" that I'll use as a constant.

This. And you are saying what you want with clarity and precision. It has semantics. It is not a one-fit-all solution.

    
23.09.2018 / 12:58