1

Per cplusplus.com, here, the default C++11 prototype for std::max() is:

template <class T> 
const T& max(const T& a, const T& b);

In the C++14 version, however constexpr was added:

template <class T> 
constexpr const T& max(const T& a, const T& b);

Why is constexpr here and what does it add?


Note on possible duplicate

I think my question is not a duplicate of this one (Difference between `constexpr` and `const`), because I am asking about a very specific usage of constexpr, whereas that question is asking "tell me everything you know about const and constexpr". The specific usage is extremely hard to dig out of those massive answers because that other question isn't pointed enough and specific enough to drive the answers right to the point of my question.

Related:

  1. This info (this question plus what I learned from my answer and others here) just went into my answer here: MIN and MAX in C
  2. Difference between `constexpr` and `const`
  3. std::max() and std::min() not constexpr
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • [std::max() and std::min() not constexpr](https://stackoverflow.com/q/5605142/995714). And don't use cplusplus.com. It has been complained many times here due to lots of issues. See [What's wrong with cplusplus.com?](https://stackoverflow.com/q/6520052/995714). https://en.cppreference.com/ is a much more reliable source – phuclv Apr 26 '20 at 01:40
  • Does this answer your question? [Difference between \`constexpr\` and \`const\`](https://stackoverflow.com/questions/14116003/difference-between-constexpr-and-const) – JaMiT Apr 26 '20 at 02:04

3 Answers3

3

This means that the function can be used in constant expressions , for example:

constexpr int f = max(3, 4);

guarantees that f is evaluated at compile-time.

Note that a function marked constexpr may have both compile-time and run-time cases depending on the function arguments (and template parameters if it is a function template). It must have at least 1 compile-time case.

Since C++11 many standard library functions have had constexpr added .

M.M
  • 138,810
  • 21
  • 208
  • 365
  • It seems like you're contradicting yourself, no? **guarantees that f is evaluated at compile-time.** ... **may have both compile-time and run-time cases** – Gabriel Staples Apr 26 '20 at 01:31
  • 1
    I don't see any contradiction here. It `guarantees that f is evaluated at compile-time.` because all inputs to f are `constexpr`. In other cases the function will be called at compile time – phuclv Apr 26 '20 at 01:39
  • 2
    @GabrielStaples The guarantee is in that one specific example. The "may have" clause applies in all examples. – JaMiT Apr 26 '20 at 01:58
  • @GabrielStaples there will be a compilation error if `f` cannot be evaluated at compile-time – M.M Apr 26 '20 at 02:02
  • @M.M, only because the `int f` is declared `constexpr` no? – Gabriel Staples Apr 26 '20 at 02:23
  • Got it; thanks. Upvoted now that I see it all. It's subtle. I recommend making it more obvious with a couple more sentences (the content of these comments basically)--so obvious _even I_ can understand it in a single go. That's a high bar. – Gabriel Staples Apr 26 '20 at 02:45
2

constexpr indicates to the compiler that the function's result can be calculated compile-time (given that the parameters also known at compile-time). I think this topic summarizes pretty well what you want to know: Difference between `constexpr` and `const`

zgyarmati
  • 1,135
  • 8
  • 15
0

I was doing some reading and it looks like it is a modifier in this case not on the return type, but rather, on the function itself. constexpr says that max() is a constexpr function. The const T& part says that the function returns a const reference to a type T, while the constexpr part, again, modifies the function itself.

This reference (https://en.cppreference.com/w/cpp/language/constexpr) indicates through its example that a constexpr function is a function which possibly can be evaluated at compile-time, but if not at compile-time, it will be evaluated at run-time like any other normal function.

Code snippet from the above reference (asterisks added):

constN<factorial(4)> out1; // computed at ***compile time***

volatile int k = 8; // disallow optimization using volatile
std::cout << k << "! = " << factorial(k) << '\n'; // computed at ***run time***

Related:

  1. This info just went into my answer here: MIN and MAX in C
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265