Closed Madssb closed 1 year ago
Hi @Madssb!
I'm sure there's a wide range of views on this among C++ experts, but here are my two cents:
I'd focus on const before worrying about constexpr, since const is a much more widely used concept.
I would not advise "adding const wherever possible" as a rule of thumb, if that means doing it by trail and error. But as long as you know why using const makes sense for the particular variable/argument (or later: member function of a class) you are considering, then it makes good sense to do so. Because then it's a deliberate part of your code design, which not only can increase speed, but also helps communicate to others what different variables/arguments are intended for.
Personally I've rarely used constexpr, but I probably should use it more. But I'd suggest only adding that towards the very end of your code development, when you know which functions in your program should only depend on values that are known at compile time (i.e. values hard-coded in your program, as opposed to values derived from e.g. command line input or other sources of input at run time.)
Hi, Is it good practice to use const and/or constexpr everywhere i physically can? (for variables that will never be redefined of course)
Is there such a thing as excessive use of const/constexpr?