1

If I had to run functions in the global scope e.g. in order to populate a static container in several cpp-files, then each time such a function is invoked I would have to initialize a new variable as well, even though I do not need them.
Here is a simplified example for such dummy variables.

static bool foo() { /*some code*/ return true; }
static bool foo2() { /*some code*/ return true; }
static bool bDummy1 = foo();
static bool bDummy2 = foo2();

Technically bDummy1 and bDummy2 are superfluous, but because of the C++ syntax it is still required. Why is there no other way to solve this?
I know it creates very little overhead to create such superfluous variables and the static keyword makes them only locally visible in cpp-files, but that is still not a good coding style.

9
  • Without the assignment calls to these functions look like (duplicate) declarations of them. Commented Dec 5, 2024 at 17:38
  • Because C, and then C++, chose to have runable code only in function scope. FWIW you only need one variable: static auto dummy = func1(), func2(), ..., funcN(); Commented Dec 5, 2024 at 17:38
  • Translate "invoke functions globally" to "run functions before program start" In C, you can't do this. I'd imagine it didn't occur to anyone to do this in a procedural language and it would have complicated the simple design. In c++, I believe allowing what you are doing is something of a consequence of global objects needing constructors run, forcing more complicated startup code to be invoked before the official program entry point (main). Note that what you are doing is subject to the Static Initialization Order Fiasco. Commented Dec 5, 2024 at 18:05
  • 2
    @cockatiel "in order to populate a static container" - Why not initialize it by calling the right function? static container_type container = populate_container(); Commented Dec 5, 2024 at 18:43
  • 3
    " I don't see any Init Order Fiasco problem here" That entirely depends on what /*some code*/ actually does. Since you want to populate a globally unique object, if any other static initialization relies on that object you may fall into the init order problem. Commented Dec 5, 2024 at 19:05

2 Answers 2

2

each time such a function is invoked I would have to initialize a new variable as well

You don't need several variables by TU, only one is sufficient:

static bool bDummy1 = foo();
static bool bDummy2 = foo2();

can be replaced by

static bool bDummy = foo(), foo2();

Why is it not possible to invoke functions globally without initializing new variables?

gcc/clang have __attribute__ ((constructor)) to solve that issue

Demo Multi-files Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Your solution does not work, if the user has to invoke foo() in separate cpp-files as I already wrote in my opening post.
Added a Multi-files Demo for the attribute solution.
I have accepted your answer, but I am disappointed that this is not part of the C++ standard.
1

I think its mandatory in C++. It can cause some confusion and harder to maintain the code. You can try other methods like

class Initializer {

public:

    static void init() {
        // Initialization code
    }
};

static bool dummy = (Initializer::init(), true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.