23
votes

When you attempt to use constexpr with main like this:

constexpr int main()

gcc and clang complain:

error: cannot declare '::main' to be inline

error: 'main' is not allowed to be declared constexpr

Let's see what requirements for constexpr function are:

A constexpr function must satisfy the following requirements:

  • it must not be virtual
  • its return type must be LiteralType
  • each of its parameters must be literal type

What is LiteralType?

A literal type is any of the following

  • void(since c++14)
  • scalar type
  • reference type
  • an array of literal type

What must the function body include?

  • null statements
  • static_assert declarations
  • typedef declarations and alias declarations that do not define classes or enumerations
  • using declarations
  • using directives
  • exactly one return statement that contains only literal values, constexpr variables and functions.

The following examples:

constexpr int main() { ; }

constexpr int main() { return 42; }

constexpr int main() {
// main defaults to return 0 
}

seems to fit all these requirements. Also with that, main is special function that runs at start of program before everything else. You can run constexpr functions from main, and in order for something marked constexpr to be constexpr, it must be run in a constexpr context.

So why is main not allowed to be a constexpr?

4
It seems to me like there is an obsession with some people to use new language features everywhere, regardless of if it makes sense. - Tim Seguine
Will this be the feature required to get rid of fork and exec to just execute /bin/false? - Johannes Schaub - litb
Also argc and argv can never be known at compile-time. - StackedCrooked
@JohannesSchaub-litb: you could actually optimize away any calling of /bin/false! - xtofl
the problem isn't with the definition of constexpr, it is with the definition of main - Glenn Teitelbaum

4 Answers

38
votes

No, this is not allowed the draft C++ standard in section 3.6.1 Main function paragraph 3 says:

[...]A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill-formed.[...]

main has to be a run-time function and as Lightness says it makes no sense since you can't optimize main away.

27
votes

The standard gives the precise signature for main, so the compiler is allowed to reject other signatures. Even more specifically, it prescribes that main cannot be constexpr, static, or some other things.

If you're wondering why, the compiler is allowed to insert code at the beginning of main (to do stuff like initialize global variables, etc.) which could make it non-constexpr (which is why e.g. a program is not allowed to call main explicitly).

16
votes

It doesn't make any sense to declare main as constexpr for two reasons: 1) It is a run-time function. 2) it may not be called from other functions or recursively.

1
votes

In my opinion the reason is that it makes no sense to declare main() as a constexpr and the standards committee want the C++ programming language to make sense.

The function main() is a special function that deals with program entry-point initialization - it is not sensible to use it to calculate compile-time values.