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

Asked

Viewed 142 times

4

I haven’t been in touch with C++, 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, example:

#define MAX_ENTRIES = 10

So, the Visual Studio that recommended replacing the macro by 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 chunk of code that the compiler will replace every time it finds referenced in the middle of the code, right?

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

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

1 answer

4


When it is 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 resource, it’s a legacy resource of C that stayed in C++.

Unless you really don’t have another way, you shouldn’t use macros. And even if you don’t, maybe you should rethink what you’re doing.

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

The example used would suffice a const that has always existed in C++. Even many already consider the use of ALL_CASE inappropriate 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 is pre-processor thing that always comes with C).

In some cases the constexpr is mandatory because the compiler needs to know that something is a expression constant that can be solved at compile time (simply speaking) and it should do it for use elsewhere. This is a constexpr:

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

I put in the Github for future reference.

If n receive a value that is constant and solved at compile time it is possible to delete the call to this function altogether, it is more than inline, it will not even run running, it executes during the build and gets the result.

Templates need much of its use.

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

Right, it’s a text substitution without any semantics, problem is yours will work and result in what you expect.

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

That. And you’re saying what you want with clarity and precision. It has semantics. It’s not a solution one-fit-all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.