问题
Is the main()
function odr-used? E.g in the simple program like this:
int main()
{
}
回答1:
No, it is not. Not in your simple program.
[basic.def.odr]
3 A function whose name appears as a potentially-evaluated expression is odr-used if it is the unique lookup result or the selected member of a set of overloaded functions ([basic.lookup], [over.match], [over.over]), unless it is a pure virtual function and either its name is not explicitly qualified or the expression forms a pointer to member ([expr.unary.op]).
main
does not appear in a potentially evaluated expression, as such it is not odr-used. Furthermore, we must consider the following:
[basic.start.main]
3 The function main shall not be used within a program.
Whether or not that applies to simply calling main, or any use, is maybe debatable, but it certainly limits your options to odr-use main
in any program.
It's worth keeping in mind when analyzing odr-use that it is the program that odr-uses entities (or not uses them). Outside forces (such as the implementation using main
as the entry point) are not subject to that debate.
来源:https://stackoverflow.com/questions/54166888/is-the-main-function-odr-used