5

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.

iohsfush
  • 107
  • 8
  • 1
    Firstly, C/C++ is not a programming language but two. Generally, pick one language. – Ulrich Eckhardt Oct 12 '19 at 09:02
  • 1
    How about using macros? You should use wisely, though. – user5876164 Oct 12 '19 at 09:04
  • 2
    You can use an old (K&R) C compiler. In K&R C, the compiler would ASSUME any undeclared function that is used returns `int` and has a variable argument list. However, relying on that is discouraged in modern C and C++, (anything more recent than the early 1990s) because it gives lots of opportunities for programmer error (e.g. calling a function with the wrong arguments, and assigning the return value to a variable of inappropriate type, both of which often give undefined or otherwise erroneous behaviour). – Peter Oct 12 '19 at 09:06
  • @Peter this is more an answer than a comment; you might want to format it as an answer. – anatolyg Oct 12 '19 at 09:09
  • 1
    Currently, C++ build process is horrendously bad - compilation time is ridiculous for large code bases. If one didn't have to declare functions prior to use it would cause an even bigger disaster in the build process. So no, you cannot. Just write `void foo();` - support for otherwise would take more time from you. – ALX23z Oct 12 '19 at 09:27
  • Can't you just define your functions before their usage? (Above `main()` in this example). Any decent editor supports code folding so you can fold up the body of the function in case you find it distracting. – brainplot Oct 12 '19 at 11:41

4 Answers4

3

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • That's a great Idea! Thanks. – iohsfush Oct 12 '19 at 09:21
  • It is good to remind that this is a really bad practice if you want to write efficient and readable code and want to have small binaries. Since defining a member function inside a class instructs the compiler imlicitly to use inlining (replace function calls with function definition at compile time), it repeats the same code which increases the size of the binary and it is unlikely that your code will be found in the cache. – Berat Postalcioglu Oct 12 '19 at 09:38
  • 3
    @BeratPostalcioglu compilers aren't required to do "inlining" (actually replace calls with the function body) for all inline functions. I am sure the compiler knows its way around and how to optimize code better than most programmers do. The only actual way to find out if this code is more/less efficient for you is to benchmark on your machine with your compiler instead of doing premature micro optimizations. – Aykhan Hagverdili Oct 12 '19 at 09:45
  • 3
    Great idea? Great hack! If someone prefers "unusual" solutions, this one goes to one of the first places :-) – Klaus Oct 12 '19 at 10:12
  • @Ayxan It is a fact that using ‘long’ inline functions blindly produces bigger binaries and inefficient code. I wanted to emphasize it is not a good habit especially in a production code. – Berat Postalcioglu Oct 12 '19 at 12:05
2

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).

Peter
  • 35,646
  • 4
  • 32
  • 74
1

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?

Klaus
  • 24,205
  • 7
  • 58
  • 113
0

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.

Pickle Rick
  • 808
  • 3
  • 6