Type violation in C ++ with declaring a function of another type?

I am new to C ++ and just trying things. I stuck with the following code:

#include<iostream> void t(){ std::cout << "func t()" << std::endl; } int main(int argc, char **argv) { int t(); //declaration of function std::cout << t() << std::endl; } 

It displays "func t () \ n6295712". I am worried about the random (?) Number printed by t ().

My question is: why is it allowed to declare a function of another return type (here: int instead of void) without any error? Is this not a type safety violation because I never defined a function with a return type of "int"?

Used compiler: gcc (Ubuntu 4.8.4-2ubuntu1 ~ 14.04.1) 4.8.4

+7
c ++ type-conversion type-safety
source share
1 answer

The only thing I can find is to note in [basic.scope.pdecl] :

Declarations of functions in a block scope and declarations of variables using the extern specifier in a scope of a block refer to declarations that are members of the encompassing namespace but do not introduce new names in this scope.

So when you write:

 void t(); int main() { int t(); // * } 

This internal declaration refers to a member of the incoming namespace. So this is equivalent to writing:

 void t(); int t(); int main() {} 

But functions cannot be overloaded only in the opposite type, therefore this code is poorly formed. Klang rejects both programs, gcc only rejects the latter. I believe this is a gcc error.

+4
source share

All Articles