使用Policy类的类的专业化

时间:2013-05-27 13:42:02

标签: c++ templates specialization

我写了以下内容:

//Policy Classes
template
<typename T>
struct CheckForZeroSpeed
{
};


template
<typename T>
struct NoCheck
{
};

//Specialization for one of the Policy Classes
template
<>
struct NoCheck<float>
{
};


//Class that uses Policy
template
<typename T,template <class C> class Policy>
class Base : public Policy<T>
{

};

//Inherited class that uses Policy
template
<typename T,template <class C> class Policy>
class Derived : public Base<T,Policy>
{
};

如何声明Base类的特化(比如说int类型)和Derived类的特化?

这也有效:

template
<>
class Base<int,NoCheck>
{
};

但这是一个基于类型和策略的专业化,它是否只能基于两个模板参数中的一个(类型)进行专业化?类似的东西:

//not compile
template
<template <class C> class Policy>
class Base<int,Policy<T>>
{
};

修改 正确的是:

//Partial specialization based on the type
    template
    <template <class C> class Policy>
    class Base<int,Policy>
    {
    };

//Partial specialization based on the Policy

    template
    <typename T>
    class Base<T,NoCheck>
    {
    };

0 个答案:

没有答案