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

后端 未结 5 705
天命终不由人
天命终不由人 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:28

    I can't think of a situation where this would be useful, but…

    #include "boost/variant.hpp"
    #include 
    #include 
    
    boost::variant, std::list >
    unknown(int someval) {
        if (someval == 1)
            return boost::variant, std::list >(
                    std::list());
        else if (someval == 2)
            return boost::variant, std::list >(
                    std::list());
    }
    

提交回复
热议问题