jeudi 7 février 2019

c++ string constant and static initialization order fiasco

I'm trying to understand when the static initialization order fiasco is a real problem. If I use a string constant like kName below would it suffer any of the problems of the static initialization order fiasco? Is this a problem in this case because an instance of Derived can be created before kName is initialized as in main.cpp?

// Base.cpp
namespace test {
class Base {
public:
  virtual ~Base() = default;

protected:
  explicit Base(const std::string &name);
};
} // namespace test

// Derived.cpp
namespace test {
static const std::string kName = "my name";

class Derived : public Base {
public:
  Derived() : Base(kName) {}
  ~Derived() override = default;
};
} // namespace test

// main.cpp
int main() {
  test::Derived instance{};
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire