0

The following code compiles fine and runs successfully.

#include <iostream>

constexpr int f();

int main()
{
    int a = f();
    std::cout << a << "\n";

    // error: ‘constexpr int f()’ used before its definition
    // constexpr int b = f();
    // std::cout << b << "\n";
}

constexpr int f()
{
    return 10;
}

Here is the output.

$ g++ -std=c++11 foo.cpp && ./a.out 
10

But when I uncomment the two lines of code in comments, I get this error.

$ g++ -std=c++11 foo.cpp && ./a.out 
foo.cpp: In function ‘int main()’:
foo.cpp:11:25: error: ‘constexpr int f()’ used before its definition
     constexpr int b = f();
                     ^

I know that I can get rid of this error quite easily by moving the definition of constexpr int f() before main().

Here are my questions:

  1. Why must a constexpr function be defined before using it in a constexpr expression?
  2. Why is declaring a constexpr function before using it in a constexpr not sufficient?
  3. Why can't the compiler figure out where the constexpr function is defined just like it can when a constexpr function or a non-constexpr function is used in a non-constexpr expression?
  4. Why is it okay to define a constexpr function after using it in a non-constexpr expression?
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
  • @NathanOliver I don't think "because the Standard says so" is a satisfying answer to any of the OP's "why ..." questions. – dyp Aug 11 '15 at 16:24
  • I'd guess one of the reasons to require a definition prior to invocation are circular dependencies such as: http://coliru.stacked-crooked.com/a/0522e72b6d686359 It might also be related to the concept of one-pass compilation. – dyp Aug 11 '15 at 16:26
  • @dyp: Then it's time to offer a bounty or otherwise draw more attention to that existing duplicate question, in order to get a better answer! :) – Lightness Races in Orbit Aug 11 '15 at 16:28
  • @LightnessRacesinOrbit It might be a very subtle difference, but Lone Learner seems to be aware of the answer to the "why" of zhengzhi liu's question. ("I know that I can get rid of this error...") – dyp Aug 11 '15 at 16:31
  • @dyp what do you want the standard committee to weigh in on why they chose this behavior? – NathanOliver Aug 11 '15 at 16:33
  • @dyp: Right but he's asking why he has to. – Lightness Races in Orbit Aug 11 '15 at 16:35
  • 3
    @NathanOliver: Asking for the rationale of certain language constraints is not unreasonable. And they're often available in the original papers. – Lightness Races in Orbit Aug 11 '15 at 16:35
  • http://wg21.cmeerw.net/cwg/issue699 contains some explanation, but the crucial parts are still missing. – dyp Aug 11 '15 at 16:43
  • The question which this post has been marked as duplicate of does not answer my question. Can this question be reopened please? – Lone Learner Aug 11 '15 at 17:28
  • @LoneLearner Only if you can make this question different from the linked one (e.g. by pointing out specifically what you'd like to know but hasn't been answered yet). Or, as LRiO suggested, add a bounty to the other question with some custom text asking for better answers. Or, go to meta.stackoverflow.com and ask how to proceed (search questions there first about similar issues). – dyp Aug 11 '15 at 18:05

0 Answers0