Choose template based on run-time string in C++

后端 未结 6 721
终归单人心
终归单人心 2020-12-14 11:39

I have an attribute vector that can hold different types:

class base_attribute_vector; // no template args

template
class raw_attribute_ve         


        
6条回答
  •  無奈伤痛
    2020-12-14 12:15

    enum class Type
    {
        Int,
        String,
        // ...
        Unknown
    };
    
    Type TypeFromString(const std::string& s)
    {
        if (s == "int") { return Type::Int; }
        if (s == "string") { return Type::String; }
        // ...
        return Type::Unknown;
    }
    
    template