受条件限制的模板专用化
Template specialization restricted by a condition
是否可以让编译器根据类型特征在模板规范之间进行选择?例如,考虑 Compare 函数的两个模板实现,一个用于顺序类型(string
s、vector
s、list
s 等),另一个用于整数类型。我们可以为每个类型类只有一个模板专用化吗?
template <class SeqT>
class Compare
{
public:
bool operator()(const SeqT& s1, const SeqT& s2) const
{
typename SeqT::const_iterator it1=s1.begin();
typename SeqT::const_iterator it2=s2.begin();
while(it1!=s1.end() && it2!=s2.end())
{
if(*it1<*it2) return true;
if(*it2<*it1) return false;
++it1; ++it2;
}
return it2!=s2.end();
}
};
template <class IntegerT>
class Compare
{
public:
bool operator()(IntegerT i1, IntegerT i2) const
{
return i1<i2;
}
};
template <class T, class Cmp = Compare<T> >
class SomeContainer
{
...
};
基本上,我正在寻找一种通过在模板参数上施加条件来部分专用化模板的方法。与第一Compare<>
一样,专用化应应用于以下类型:std::basic_string<>
、std::vector<>
、std::list<>
,第二种类型应用于以下类型:int
、unsigned
、short
、char
。这可能吗?
这是我对另一个问题的回答,但这正是你需要的。它使用 SFINAE 仅为针对条件测试 true 的模板参数创建模板专用化(例如对特定类型进行检测)。
https://stackoverflow.com/a/20898554/2805305
编辑
但是,我如何才能准确地指定第一个比较专业化可以应用于例如 std::basic_string 和 std::vector?
您创建一个特征,告诉您 T 是向量还是basic_string或列表:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <complex>
#include <type_traits>
using namespace std;
template <class T>
struct is_seq : std::false_type {
};
template <class T>
struct is_seq<std::vector<T>> : std::true_type {
};
template <class T>
struct is_seq<std::basic_string<T>> : std::true_type {
};
template <class T>
struct is_seq<std::list<T>> : std::true_type {
};
template <class T>
using enable_if_seq_type = typename std::enable_if<is_seq<T>::value>::type;
template <class T>
using enable_if_integral_type = typename std::enable_if<std::is_integral<T>::value>::type;
template <class T, class Enable = void>
class Compare; // <-------- define if you want a Compare for any type that doesn't match any specialization
template <class T>
class Compare<T, enable_if_seq_type<T>> { // specialization for T a vector, string or list
public:
void foo() {
cout << "vector, string and list specialization" << endl;
}
};
template <class T>
class Compare<T, enable_if_integral_type<T>> { // specialization for T an integral type
public:
void foo() {
cout << "integral specialization" << endl;
}
};
int main() {
cout << std::boolalpha;
cout << is_seq<int>::value << endl; // false
cout << is_seq<std::vector<int>>::value << endl; // true
Compare<int> c1; // uses second specialization
c1.foo(); // output "integral specialization"
Compare<std::vector<int>> c2; // uses first specialization
c2.foo(); // output "vector, string and list specialization"
//Compare<std::complex<int>> c3;
// compile error if you just declare and not define the generic Compare.
// If you define the generic Compare, this will compile and it will use
// that definition
return 0;
}
http://ideone.com/JUbwla
如果希望能够实例化任何其他类型的类Compare
,则定义第一个(常规)Compare
声明。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- .cpp和.h文件中的模板专用化声明
- 在没有太多条件句的情况下,我如何避免被零除
- 调用专用模板时出错"no matching function for call to [...]"
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 无论条件是否为true,if总是在c++中执行
- 模板专用化(按容器):value_type
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 基于模板值的条件变量
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 将按位if条件转换为普通if条件
- 条件断点在不应该触发时触发
- 为什么简单的算术减法在"if"条件下不起作用?
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- 具有多个条件的默认模板专用化
- 如何将此条件放入模板部分专用化中?
- 受条件限制的模板专用化
- 具有条件的模板专用化
- 非模板类方法的条件模板专用化