Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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

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_Specialization - Fatal编程技术网

C++ 类标志的模板专门化

C++ 类标志的模板专门化,c++,templates,specialization,C++,Templates,Specialization,我想知道下面的部分专门化,即我基于通用列表模板定义有序和无序列表,是否是正确、有效的方法 template <typename T, bool O> class List; template <typename T> class UList: public List<T,false>{}; template <typename T&g

我想知道下面的部分专门化,即我基于通用列表模板定义有序和无序列表,是否是正确、有效的方法

template <typename T, bool O> class List;                                       
template <typename T> class UList: public List<T,false>{};                      
template <typename T> class OList: public List<T,true>{};
模板类列表;
模板类UList:public List{};
模板类OList:public List{};

谢谢,

我认为您希望通过减少用户最终将在其代码中使用的类的模板参数数量来减少类模板的泛型性。如果是这样的话,那么在C++03和C++98中,这就是您可以做的事情,即定义从更泛型的类派生的更少泛型的类

然而,在C++11中,您不需要定义新的类模板。相反,您可以创建,同时减少模板参数的数量,如下所示:

template <typename T> 
using UList = List<T,false>;                      

template <typename T> 
using OList = List<T,true>;

在中,我刚刚学习了定义类型/别名的新方法,以前我们使用
typedef
来定义类型/别名:

using语法也可用作C++11中的类型别名:

typedef void (*Type)(double);         // Old style
using OtherType = void (*)(double);   // New introduced syntax

对于这种情况,我将使用一个策略类来定义如何将内容插入到单个列表中,例如

struct DefaultInsertionPolicy
{
  template <typename ContainerType, typename ValueType>
  static void insert(ContainerType& container, ValueType const& cValue)
  {
    container.insert(container.end(), cValue);
  }
};

struct SortedInsertionPolicy
{
  template <typename ContainerType, typename ValueType>
  static void insert(ContainerType& container, ValueType const& cValue)
  {
    // I'm using lower_bound here, but do what is necessary for you
    typename ContainerType::iterator ft = std::lower_bound(container.begin(), container.end(), cValue);
    container.insert(ft, cValue);
  }
};

template <typename T, typename InsertPolicy = DefaultInsertionPolicy>
class List
{
  :
  // insert function
  void insert(T const& cValue)
  {
    InsertPolicy::insert(*this, cValue); // delegate to the policy to do the insert
  }

  void insert(iterator iPos, T const& cValue)
  {
    // do the real insertion at the provided position.
  }
};
struct DefaultInsertionPolicy
{
模板
静态void插入(ContainerType和container、ValueType const和cValue)
{
container.insert(container.end(),cValue);
}
};
结构分类策略
{
模板
静态void插入(ContainerType和container、ValueType const和cValue)
{
//我在这里使用下限,但是做你需要做的事情
typename ContainerType::iterator ft=std::下限(container.begin()、container.end()、cValue);
容器。插入(英尺,C值);
}
};
模板
班级名单
{
:
//插入函数
无效插入(T常量和C值)
{
InsertPolicy::insert(*this,cValue);//委托给策略执行插入
}
无效插入(迭代器iPos、T常量和C值)
{
//在提供的位置进行实际插入。
}
};
所以真正的类型可能是

typedef List<some_type> UList; // insertion order
typedef List<some_type, SortedInsertionPolicy> OList; // sorted list
typedef列表UList;//插入顺序
typedef List OList;//排序表

如果不知道
列表是如何实现的,很难说。您最好使用定义元素插入方式的策略。列表实现如何影响决策?在不知道如何实现的情况下,我们如何告诉您该策略是否有效(或正确)?您是对的,但是是否有一种通用的方法来确定使用此方法可以有效使用哪些类?
typedef List<some_type> UList; // insertion order
typedef List<some_type, SortedInsertionPolicy> OList; // sorted list