Is it possible to choose a C++ generic type parameter at runtime?

后端 未结 5 702
天命终不由人
天命终不由人 2021-02-14 07:52

Is there a way to choose the generic type of a class at runtime or is this a compile-time thing in C++?

What I want to do is something like this (pseudocode):

         


        
5条回答
  •  星月不相逢
    2021-02-14 08:11

    The closest you'll get is:

    template 
    void do_stuff_with_list
    {
        list myList;
        ...
    }
    
    enum Type
    {
       Integer = 1,
       String
    };
    
    void do_stuff(Type type)
    {
        switch (type)
        {
        case Integer:
            do_stuff_with_list();
            break;
        case String:
            do_stuff_with_list();
            break;
        };
    }
    

提交回复
热议问题