partial type as template argument c++ [duplicate]

喜欢而已 提交于 2019-12-02 16:40:17

问题


Simply, can I pass std::vector as a template argument. Following example list usage

tempate<typename container_t, typename value_t>
struct container_types
{
  typedef container_t<value_t> value_container_t;
  typedef container_t<pair<value_t> pair_container_t;

};

I wish to pass container_types to generate final two data types. If this is not feasible, then how can I achieve the same.


回答1:


Yes, like this

#include <iostream>
#include <vector>

template <template<typename T> class container_t, typename value_t>
struct container_types
{
    typedef container_t<value_t> value_container_t;
    typedef container_t<std::pair<value_t,value_t > > pair_container_t;
};

template <typename T>
using my_vector = std::vector<T>;

int main(int argc,char** argv)
{
    container_types<my_vector,int>::value_container_t buff(100);
    std::cout << buff[50] << std::endl;
}

Note that I had to wrap std::vector with my_vector, as std::vector actually has several template arguments.



来源:https://stackoverflow.com/questions/22387356/partial-type-as-template-argument-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!