2

Disclaimer: This question is a bit complicated because it's several questions in one, but they're all related to the same sort of concept/issue.

Premise: consexpr functions may only consist of a single return statement.

They can call other functions and use conditionals, but theoretically they should demonstrate functional purity, and thus the results should be cachable (by the compiler, at compile time) in some sort of map so that the compiler doesn't have to constantly re-evaluate the same function.

Question(s): Is this assumption correct or is there something I haven't considered that makes it impossible to cache the result of a constexpr function? If no, does this mean that constexpr functions have to be calculated every time they're used?

What about templates? Are constexpr values on templates cachable or do they also have to be recalculated each time?

Pharap
  • 3,826
  • 5
  • 37
  • 51
  • `consexpr` functions can often be evaluated at compile time; that's kind of the point of the exercise. In that case, the executable code ends up with just the constant that is the result of evaluating the function call, but not the code for the function itself. That's ultimate caching. Or are you asking whether the compiler can optimize multiple evaluations at compile time? Probably, but I'm not sure why you'd care. – Igor Tandetnik Apr 01 '17 at 21:45
  • Why should you care? What difference does it make to your program? – n. m. could be an AI Apr 01 '17 at 21:46
  • @IgorTandetnik I mean does the compiler assume the function is pure, cache the result for a given input using a map, and consult the cache (i.e. the map) from then on instead of continually re-evaluating the function? – Pharap Apr 01 '17 at 21:47
  • 3
    @n.m. Are SO users not allowed to ask questions based on interest alone? Does there always have to be some tangible production issue at the root of the question? – Pharap Apr 01 '17 at 21:50
  • Of course SO users are allowed to ask all kinds of questions. However it is useful to think about validity of your question before asking. Would you be able tell a compiler that does constexpr caching, whatever it is, from one that doesn't? If yes, how? If not, what does your question *mean*? – n. m. could be an AI Apr 01 '17 at 21:57
  • As far as I know `constexpr` functions should be evaluated into a const value at compile time, like `sizeof` (except in the case of VLA). – Colliot Apr 01 '17 at 21:58
  • 1
    `constexpr` functions are not required to be pure. It's only required that the function have at least one set of arguments for which it can be evaluated at compile time (and even for that, no diagnostic is required). Among other things, `constexpr` functions can throw exceptions. See http://en.cppreference.com/w/cpp/language/constexpr , look for `constexpr char operator[]` in the example thereat. – Igor Tandetnik Apr 01 '17 at 22:01
  • 1
    Short answer: Sure. Real answer: [On stateful constexpr functions](http://stackoverflow.com/a/41939866/990142). – rwols Apr 01 '17 at 22:03
  • The question is pretty vague ... compilers are allowed to do anything at all so long as the output of the program matches the output specified by the C++ Standard for that code. If a compiler can figure out that repeating some procedure wouldn't change the output then it can decide to not repeat the procedure, and so on. IMO it would be better if you posted a specific code example of what you are asking about. – M.M Apr 01 '17 at 22:21

1 Answers1

1

I don't believe constexpr functions are required to be pure - at least, not for all possible arguments. Consider:

#include <iostream>
#include <stdlib.h>

constexpr int f(int n) { return n == 0 ? 42 : rand(); }

template <int n> void g() {}

int main()
{
    g<f(0)>();  // OK
//    g<f(1)>();  // error
    std::cout << f(0) << ' ' << f(1) << ' ' << f(2);
}

Output: 42 1804289383 846930886. Demo

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • That is some very odd behaviour. I'm pretty sure `rand` isn't `constexpr` so I find it very odd that a `constexpr` function would accept it as valid. – Pharap Apr 02 '17 at 01:04
  • 2
    @Pharap there is most definitely no requirement that a constexpr function only contains calls to other constexpr functions. – n. m. could be an AI Apr 03 '17 at 20:14