How to share global constants with minimum overhead at runtime?

后端 未结 5 604
[愿得一人]
[愿得一人] 2021-01-19 05:21

I am using C++11. I am not allowed to use external libraries like boost etc. I must use STL only.

I have a number of events, which must be identified as string const

5条回答
  •  庸人自扰
    2021-01-19 06:16

    You can have a struct of static strings:

    struct MyNames
    {
        static const std::string name1;
    };
    

    And in a cpp:

    const std::string MyNames::name1 = "foo";
    

    You can then access the names from all your required locations. In C++17, you would have used string_view instead to avoid object construction. But this seems to be a duplicate of this answer, basically: https://stackoverflow.com/a/55493109/2266772

提交回复
热议问题