Templates and STL

前端 未结 6 1771
情歌与酒
情歌与酒 2021-01-05 18:31

The following code represents a container based on std::vector

template 
struct TList
{
    typedef std::vector  Type;
};


         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 18:52

    You can use template template parameters as others have mentioned here. The main difficulty with this is not that dissimilar container types have dissimilar template parameters, but that the standard allows the standard containers, like vector, to have template parameters in addition to the documented, necessary ones.

    You can get around this by providing your own subclass types that accept the appropriate template parameters and let any extras (which have to have defaults) be filled in my the implementation:

    template < typename T > struct simple_vector : std::vector {};
    

    Or you can use the templated typedef idiom:

    template < typename T > struct retrieve_vector { typedef std::vector type; };
    

提交回复
热议问题