How is it possible to declare nothing inside main() in C++ and yet have a working application after compilation?

C++

C++ Problem Overview


In an interview I was confronted with a question such as this:

> Your friend has given you a single source code file which prints the > Fibonacci numbers on the console. Note that the main() block is > empty and doesn't have any statements inside it.

> Explain how this is possible (hint: global instance!)

I really want to know about this, how such a thing can be even possible!

C++ Solutions


Solution 1 - C++

It is most likely implemented as (or a variant of it):

 void print_fibs() 
 {
       //implementation
 }

 int ignore = (print_fibs(), 0);

 int main() {}

In this code, the global variable ignore has to be initialized before entering into main() function. Now in order to initialize the global, print_fibs() needs to be executed where you can do anything — in this case, compute fibonacci numbers and print them! A similar thing I've shown in the following question (which I had asked long back):

Note that such code is not safe and should be best avoided in general. For example, the std::cout object may not be initialized when print_fibs() is executed, if so then what would std::cout do in the function? However, if in other circumstances, it doesn't depend on such initialization order, then it is safe to call initialization functions (which is a common practice in C and C++).

Solution 2 - C++

Hope this helps

class cls
{
  public:
    cls()
    {
      // Your code for fibonacci series
    }
} objCls;

int main()
{
}

So as soon as a global variable of the class is declared, the constructor is called and there you add the logic to print out the Fibonacci series.

Solution 3 - C++

Yes it is possible. You need to declare a global instance of an object that calculates the Fibonacci numbers in the object constructor.

Solution 4 - C++

I know some examples like that you tell. One way to get it is using the template metaprogramming. Using it you can move some compute process to the compilation.

Here you can get an example with the Fibonacci numbers

If you use it in a static class constructor and you can write the numbers without need to write any code in the main function.

Hope it helps you.

Solution 5 - C++

Things can happen during initialization of global/static variables. The code will be trigger at the application start.

Solution 6 - C++

All[*] constructors for file-scope objects get called before reaching main, as do all initializer expressions for non-object file-scope variables.

Edit: Also, all[*] destructors for all file-scope objects get called in reverse order of construction after main exits. You could, theoretically, put your fibonacci program in an object's destructor.

[*] Note that 'all' ignores the behavior of dynamically loading and unloading libraries that your program wasn't directly linked with. Those technically are outside the base C++ language, though.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHosseinView Question on Stackoverflow
Solution 1 - C++NawazView Answer on Stackoverflow
Solution 2 - C++SakshamView Answer on Stackoverflow
Solution 3 - C++Mr. BeerView Answer on Stackoverflow
Solution 4 - C++superarceView Answer on Stackoverflow
Solution 5 - C++log0View Answer on Stackoverflow
Solution 6 - C++Joe ZView Answer on Stackoverflow