如何专门化模板类型的普通类中的模板成员
How to specialize a template member in a normal class for a template type
我有一个类,它不是模板,但有一个模板函数,如下所示:
class Table {
public:
template <typename t>
t Get(int, int);
};
我想对模板类型进行专门化比如像这样定义的fixed_string
template <int max>
class fixed_string {
};
我该怎么做呢?
正如@ForEveR所指出的,没有局部函数模板专门化,您只能为它提供完整的专门化版本。如:
template <>
fixed_string<9> Table::Get<fixed_string<9>>(int, int);
然后用
调用table.Get<fixed_string<9>>(0, 0);
但是可以重载函数模板,例如:
class Table {
public:
template <typename t>
t Get(int, int);
template <int max>
fixed_string<max> Get(int, int);
};
和
Table table;
table.Get<9>(0, 0);
或
class Table {
public:
template <typename t>
t Get(int, int);
template <int max, template<int> class T=fixed_string>
T<max> Get(int, int);
};
和
Table table;
table.Get<9>(0, 0); // or table.Get<9, fixed_string>(0, 0);
生活如果我理解对了你的问题,像这样:
Table table;
table.Get<fixed_string<100>>(3,4);
没有局部函数模板特化,所以你只能为已知max的fixed_string特化。
template<>
fixed_string<100> Table::get(int, int)
{
}
定义:
template<int max>
fixed_string<max> Get(int, int);
调用:
Table obj;
fixed_string<20> str = obj.Get<20>(1, 2);
相关文章:
- 根据模板类型选择类模板的成员类型?
- 函数模板签名中忽略的成员类型def 的访问说明符
- 在C++如何从数组中提取成员并返回成员类型的数组?
- C++从成员类型中扣除类型的功能模板?
- 无法初始化以 std::byte 作为成员类型的位字段
- 构造函数可以更改默认成员类型吗?
- 使用相同方法但不同成员类型构建类的最佳方法
- 允许通过指向方法的成员参数的指针来推断模板参数所指向的成员类型
- 使用 std::条件根据模板参数选择成员类型
- CRTP:如何推断要用作返回类型的成员类型?
- 对成员类型的成员方法使用 std::result_of<>
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 如何在不指定不必要的模板参数的情况下使用模板类的成员类型
- 基类数据成员类型取决于派生类
- C 多态性:允许模棱两可的成员类型
- 使用使用成员类型别名的构造函数来推论类模板参数
- 获取与在模板参数中传递的函数成员类型相同的类
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 将联合强制转换为其成员类型之一