main in namespace

前端 未结 3 708
鱼传尺愫
鱼传尺愫 2021-02-05 01:43

Why doesn\'t this compile? The linker can\'t find main, but why is this the case?

namespace somenamespace{

int main(void){
 return 0;
}

}
3条回答
  •  不要未来只要你来
    2021-02-05 02:12

    3.6.1/1 - "A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: in a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]

    Your example has 'main' (intended as the program entry point) as a namespace function and hence your code is ill-formed. This does not mean that a function 'main' can not be defined as you did. It just means that a global namespace scope definition of 'main' in accordance with the Standard defined signature is required for a free standing program. hosted program

提交回复
热议问题