jotik
jotik

Reputation: 17900

Can main() have an exception specification?

In standard C++, can the main function have an exception specification?

For example, is the following legal?

int main() noexcept {}

Upvotes: 1

Views: 102

Answers (1)

jotik
jotik

Reputation: 17900

Yes, it completely is legal. There is no wording in the C++ standard (in [basic.start.main], [except.spec], or elsewhere) that prohibits this.

Even in C++17 and later where exception specifications are part of the function type, main is only restricted in its linkage and return type according to [basic.start.main#2]:

An implementation shall not predefine the main function. This function shall not be overloaded. Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined.

Upvotes: 4

Related Questions