Templates and STL

前端 未结 6 1768
情歌与酒
情歌与酒 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:45

    Yes and no.

    You can use a template template parameter, e.g.,

    template  class Container>
    struct TList { /* ... */ };
    

    However, in general, template template parameters are not particularly useful because the number and types of the template parameters have to match. So, the above would not match std::vector because it actually has two template parameters: one for the value type and one for the allocator. A template template parameter can't take advantage of any default template arguments.

    To be able to use the std::vector template as an argument, TList would have to be declared as:

    template  class Container>
    struct TList { /* ... */ };
    

    However, with this template, you wouldn't be able to use the std::map template as an argument because it has four template parameters: the key and value types, the allocator type, and the comparator type.

    Usually it is much easier to avoid template template parameters because of this inflexibility.

提交回复
热议问题