Closed LocalSpook closed 1 week ago
Other than consistency (which is a good reason on its own), are there actual use cases for this?
Does constexpr main make sense given that args are runtime parameters by design?
The best example I've been able to come up with is the POSIX test
/[
command. It exists to return 0 if the given condition is true, 1 if false. With constexpr main
, you could test a subset of its functionality at compile time.
static_assert(main("[", "-2", "-le", "0", "-a", "foo", "=", "foo", "]") == 0);
@LocalSpook True but in such case you might aswell go the 2-layer main approach and just test the inner function.
I think that the workaround to this issue is so easy to come up with and so trivial to implement that it is not worth the bureaucratic effort of changing the standard.
On the contrary, is there any "official justification" why the main function has such special rules?
Thanks! I'm not against doing this, but I don't think it's one of the more pressing safety or simplicity issues with C++ and I need to prioritize... so I'll close this for the time being in favor of other bug fixes and new features. But I appreciate the suggestion, and thanks for understanding!
In C++,
main
cannot be called from user code and cannot be constexpr (section [basic.start.main] of the standard).The workaround is to define
main
to do nothing but delegate to an ordinary function without these restrictions:I propose that cppfront does this automatically, making something like the following code valid:
This would reduce
main
's inconsistency and eliminate potential undefined behavior.Cppfront would have to
main
;main
in Cpp2 code to the custommain
; andargs_t
constexpr.