1

我写了以下内容:

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

如何声明基类的特化(比如说int类型)和派生类的特化?

这也有效:

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

0 回答 0