How to share global constants with minimum overhead at runtime?

后端 未结 5 615
[愿得一人]
[愿得一人] 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:14

    Global const std::string has one drawback it need processing during startup and creates copy of string literal.

    The linked SO answear uses constexpr std::string_view and this is cool solution since constructor is constexpr so nothing have to be done on startup. Also it doesn't create any copy. Problem is that this is C++17

    Use of const char [] (or auto or constexpr) is old proven solution. You can compare std::string with it without any extra overhead.

    You can create header file for all that strings and let linker to remove all duplicates. It was working like that in old C++.

提交回复
热议问题