How to force the compiler to declare functions in C/C++ itself?

Viewed 515

The problem:
I hate writing headers or declaring my functions if my computer can do it faster.
And it is the case.
What I tried to do:
I tried to declare a function foo after the main function.
But the compiler returns an error:
error: ‘foo’ was not declared in this scope
The code:

#include <iostream>

//no function declaration is allowed, please.
//no other header is allowed, please.

void main() {
    foo();
}

void foo() {
    std::cout << "The compiler is smart now!" << std::endl;
}

I accept to change the compiler if gcc/g++ is not able to compile this c++ code.
Any response will be greatly thanked.

4 Answers

This is not possible with freestanding functions as the languages require at least a function prototype to call it. Having said that, in C++ you can use classes to achieve a similar result:

class Main {
public:
    static int main() {
        foo();
        return 0;
    }
    static void foo() { }
};

int main() {
    return Main::main();
}

Whether or not this is a good practice is a different discussion.

You can use an old (K&R) C compiler. In K&R C, the compiler would implicitly ASSUME any undeclared function that is used returns int and has a variable argument list.

However, that feature has never been supported in C++. It is is also strongly discouraged in modern C (anything more recent than the early 1990s) and disallowed in C standards from 1999. The reason is that such a feature provides lots of opportunities for programmer error (e.g. calling a function with the arguments of incorrect type, and/or assigning the return value to a variable of inappropriate type, both of which often give undefined or otherwise erroneous behaviour).

I believe you are on the wrong way.

That your compiler did not "know" that you have a function declaration is only half of the story. If you only declare the function than use it and later define it, maybe in a different translation unit, the compiler is not able to inline your code during optimization. Modern compilers can use link time optimization, but that feature must be enabled on command line.

So I would advice you to reorder your code instead of using function declarations from (automated generated) headers. Simply move your definitions in front of the usage in your code.

In C++ it is sometimes useful to write "header only" code and splitting up your software in header and source file is not always the best idea. There are pros and cons for that, but using headers with full implementation offers often better optimization by inlining. As said: Also via LTO possible.

BTW: The task to generate header files from implementation is described already here: Automatically generate C++ file from header?

This isn't related to GCC, it's universal with C++. The new standard introduces modules which looks promising to fix the 'include hell' issues of the language, bit that's still not quite what you're looking for anyway. There is no way to do what you want, regardless of the compiler, without a declaration beforehand.

Related