Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 部分模板专门化_C++_Templates_Partial Specialization_Template Templates - Fatal编程技术网

C++ 部分模板专门化

C++ 部分模板专门化,c++,templates,partial-specialization,template-templates,C++,Templates,Partial Specialization,Template Templates,有以下代码: template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class Alloc=std::allocator<T>> class ContProxy { OuterCont<T, InnerCont<T, Alloc>> _conta

有以下代码:

template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class Alloc=std::allocator<T>>
class ContProxy { 
    OuterCont<T, InnerCont<T, Alloc>> _container;
};
typedef ContProxy<int, std::vector, std::list> IntCont;
对于这种情况,是否可以使用“模板”参数的部分专门化?

或者如何将其归档到最省事的地方。

简单地在类型上设置模板通常更容易。你不能用模板捕捉每一种情况——如果有人想用一个包含六个模板参数的容器怎么办?因此,请尝试以下方法:

template<typename T, template<typename, typename> class OuterCont, T*, class Alloc=std::allocator<T>>
class ContProxy { 
    OuterCont<T, T*> _container;
};
template <typename T, typename C>
struct ContProxy
{
    typedef C                    container_type;
    typedef typename C::second_type second_type;

    container_type container_;
};

ContProxy<int, MyContainer<int, std::list<int>> p;
模板
结构ContProxy
{
类型定义C容器类型;
typedef typename C::second_type second_type;
容器类型容器;
};

ContProxy我也同意kerrek的解决方案,但除此之外,我能想到的最好的办法就是这个

问题是InnerCont在基模板中声明为模板类型,因此您不能再将其专门化为原始指针。因此,您可以创建一个表示指针的虚拟模板并使用它

template<typename,typename> class PtrInnerCont; //just a dummy template that does nothing

template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class Alloc=std::allocator<T>>
class ContProxy  { 
    OuterCont<T, PtrInnerCont<T, Alloc>> _container;
};
typedef ContProxy<int, std::vector, std::list> IntCont;

template<typename T, template<typename, typename> class OuterCont, class Alloc>
class ContProxy<T, OuterCont, PtrInnerCont, Alloc> { 
    OuterCont<T, T*> _container;
};

typedef ContProxy<int, std::vector, PtrInnerCont> MyCont;
模板类PtrInnerCont//只是一个不做任何事情的虚拟模板
模板
类ContProxy{
外部集装箱;
};
typedef ContProxy IntCont;
模板
类ContProxy{
外部集装箱;
};
霉菌感染;

你不能真正做你已经在做的事情。不是以标准的方式。C++容器不使用相同的模板参数。

这样做:

template< typename T, 
          template<typename, typename> class OuterCont,
          template<typename, typename> class InnerCont, 
          class Alloc=std::allocator<T>>
class ContProxy { 
    typename OuterCont<T, typename InnerCont<T, Alloc>::type>::type _container;
};
template
类ContProxy{
typename-OuterCont::type\u容器;
};
然后可以创建不同的容器生成器,如下所示:

template < typename T, typename A = std::allocator<T> >
struct vector_gen { typedef std::vector<T,A> type; };
template
结构向量{typedef std::vector type;};
或者您的指针:

template < typename T, typename Ignored >
struct pointer_gen { typedef T* type; };
模板
结构指针{typedef T*type;};