Given a vector of strings which is known at compile-time (and say some numbers they should map to), I wish to create such a map (f.e. unordered_map
) at compile-time. The goal is to start up quickly and perform just look-ups at runtime. Take this example:
enum category {fruit, vegetable};
const std::vector<std::string> fruits = {"apple", "pear", "orange"};
const std::vector<std::string> vegetables = {"cucumber", "zucchini", "tomato"};
const std::unordered_map<std::string, category> lookup_category = // ?
However, constexpr
forbids the use of non-literals. Templating is a solution, however it is a massive headache to implement and maintain.
C ++(17?)在STL中是否有某些东西可以帮助我在编译时构建这种映射?