2

I have a C++ code looks like this

Parent.hpp

class Parent {
    static std::map<std::string, std::function<void()>> list;
}

Parent.cpp

#include "Parent.hpp"
std::map<std::string, std::function<void()>> Parent::list;

Child.hpp

#include "Parent.hpp"
class Child : Parent {
    static bool isRegistered = registerComponent();
    std::function<void(GameObject* go, void* arr)> add;
    static bool registerComponent();

Child.cpp

#include "Child.hpp"
    static bool Child::isRegistered = registerComponent();
    std::function<void(GameObject* go, void* arr)> Child::add = []() {
        //do something
    }
    static bool Child::registerComponent() {
        if (add) {
            list["child"] = Child::add;
            return true;
        }
        else return false
    }

is list guaranteed to be initialized before I call registerComponent()? I have read this post When are static C++ class members initialized? I think it's not guaranteed, but I am not 100% sure.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Jiehong Jiang
  • 75
  • 1
  • 7

1 Answers1

3

No it's not. The way you wrote it, there is no order of initialization guarantee between list and the static members of Child.

What is guaranteed is that static data is initialized prior to any use (according to the one definition rule, that is) of any function in their translation unit. So if you were to make registerComponent a member of Parent, everything will be alright between the Parent class and its children.

class Parent {
    static std::map<std::string, std::function<void()>> list;
  protected:
    // Implemented in the same TU where list is defined
    static bool registerComponent(std::string k, std::function<void()> v);
};

Now any child class calling registerComponent will be using a function from the translation unit that defined list, so list will definitely be initialized before the function is executed.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I have a follow-up question. What if the function is used by other translation units? Will it still be guaranteed? Thanks! – Jiehong Jiang Jun 30 '19 at 06:16
  • 1
    @JiehongJiang - Please don't incorporate *answers* into your *question*. It invalidates the answers and makes for one confusing post. – StoryTeller - Unslander Monica Jun 30 '19 at 06:18
  • @JiehongJiang - As for your followup, it doesn't matter how many TU's use the function. The static will be initialized prior to being used, guaranteed. If you still have a followup question, then please [Ask a Question](https://stackoverflow.com/questions/ask) so it may get a proper answer. – StoryTeller - Unslander Monica Jun 30 '19 at 06:20