include - C++ function declarations -
i'm newbie c++. don't understand why okay (i.e. why compiler allows it) 1 function declared twice. example, following code legal:
#include <iostream> #include <string> int hello(); int hello(); int main(){ cout << "hello, world" << endl; } int hello(){ return 1; } why compiler not complain?
in c , c++ forward declarations weak. provide formal "promise" compiler if function specified signature appears @ all, have signature specify. function not guaranteed appear: unless call or otherwise reference declared function, compiler not going complain there declaration no definition. standard requires compilers treat identical forward declarations single declaration.
unlike definitions must unique according single definition rule
3.2 no translation unit shall contain more 1 definition of variable, function, class type, enumeration type, or template
declarations merely required refer same definition, i.e. equivalent each other:
3.3.4 given set of declarations in same declarative region, each of specifies same unqualified name, shall refer same entity, or refer functions or function templates, [...]
Comments
Post a Comment