部分类型作为模板参数c ++ [英] partial type as template argument c++

查看:58
本文介绍了部分类型作为模板参数c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我可以将 std :: vector 用作模板参数.以下示例列表用法

  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;}; 

我希望传递container_types来生成最后两个数据类型.如果这不可行,那我该如何实现.

解决方案

是的,像这样

  #include< iostream>#include< vector>模板<模板<类型名称T>类container_t,类型名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;};模板< typename T>使用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;} 

请注意,我必须用 my_vector 包装 std :: vector ,因为 std :: vector 实际上有几个模板参数.

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.

解决方案

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.

这篇关于部分类型作为模板参数c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆