This looks interesting

Solving Undefined Behavior in Factories with constinit from C++20
https://www.cppstories.com/2023/ub-factory-constinit/

It would be helpful for opinions from others who are more attuned to C++ than I am.
Last edited on
Well, the entry is not overly promising:
1
2
3
4
5
6
7
8
9
static unique_ptr<ICompressionMethod> Create(const string& fileName) {
    auto extension = GetExtension(filename);
    if (extension == "zip")
        return make_unique<ZipCompression>();
    else if (extension = "bz")
        return make_unique<BZCompression>();

    return nullptr;
}


You can solve that addressed without C++20 using std::map:
1
2
3
4
5
static std::map<string_view, TCreateMethod>& get_map()
{
  static std::map<string_view, TCreateMethod> s_methods;
  return s_methods;
}
Topic archived. No new replies allowed.