使用enable_if专门化结构模板
Specializing struct template using enable_if
我正在尝试创建一个模板类,该类将实现带有不同签名的回调,具体取决于它是否与一种类型实例化,或两种。
struct NoIntermediate
{
};
template<typename R, typename I>
struct ParserCallbackSwitch
{
using type = std::function<bool(const std::string &, R&, I&)>;
}
template<typename R, typename I = NoIntermediate>
class OtherClass
{
public:
typedef ParserCallbackSwitch<R, I>::type ParserType;
}
现在,我想添加代码,以便在实例化"其他班级"时未指定,parsercallbackswitch将是:
template<typename R, typename I>
struct ParserCallbackSwitch
{
using type = std::function<bool(const std::string &, R&)>;
}
请注意,在这种情况下,ParserCallbackSwitch ::类型是一个函数,只有两个参数。
我希望能够执行以下操作:
OtherClass<int, float> p; // p::ParserType = std::function<bool(std::string &, int &, float &);
OtherClass<int> q; // q::ParserType = std::function<bool(std::string &, int &);
我无法弄清楚我是类型 NoIntermediate
的情况(即未指定)的情况下的 ParserCallbackSwitch
解决方案:基于下面的响应。这是我最终使用的代码。
struct NoIntermediate {};
template<typename R, typename I = NoIntermediate>
struct ParserCallbackSwitch
{
using type = std::function<bool(const std::string &, R&, I&)>;
};
template<typename R>
struct ParserCallbackSwitch<R, NoIntermediate>
{
using type = std::function<bool(const std::string &, R&)>;
};
template<typename R, typename I = NoIntermediate>
class OtherClass
{
public:
typedef ParserCallbackSwitch<R, I>::type ParserType;
}
so!您无法适当地专门使用模板。您正在定义两个恰好具有相同名称的无关类模板。
有多种方法可以做您建议的事情。这给出了最不专业的模板一个参数包。
#include <functional>
#include <type_traits>
template<typename... S>
struct OtherClass;
template<typename R, typename I>
struct OtherClass<R, I> {
using ParserType = std::function<bool(std::string&, R&, I&)>;
};
template<typename R>
struct OtherClass<R> {
using ParserType = std::function<bool(std::string&, R&)>;
};
int main(void) {
static_assert(std::is_same<OtherClass<int, float>::ParserType,
std::function<bool(std::string&, int&,
float&)>>(),
"Something wrong here.");
static_assert(std::is_same<OtherClass<int>::ParserType,
std::function<bool(std::string&, int&)>>(),
"Hmmmmmm.");
return 0;
}
您在参数中使用默认类型的想法也有效,但是您的语法略有关闭。这就是外观。
#include <functional>
#include <type_traits>
template<typename R, typename I = void>
struct OtherClass {
using ParserType = std::function<bool(std::string&, R&, I&)>;
};
template<typename R>
struct OtherClass<R, void> {
using ParserType = std::function<bool(std::string&, R&)>;
};
int main(void) {
static_assert(std::is_same<OtherClass<int, float>::ParserType,
std::function<bool(std::string&, int&,
float&)>>(),
"Something wrong here.");
static_assert(std::is_same<OtherClass<int>::ParserType,
std::function<bool(std::string&, int&)>>(),
"Hmmmmmm.");
return 0;
}
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何为特定数量的模板参数专门化可变参数模板结构
- 使用enable_if专门化结构模板
- g++和clang++在结构/类专门化中具有非类型参数的不同行为
- PTR到结构体和PTR到结构体的向量的模板方法专门化
- 使用模板类作为参数专门化模板结构
- 一个模板专门化用于pod,一个模板专门化用于类层次结构,在其他情况下出现错误
- 如何从结构中提取索引最高的专门化
- 不能专门化结构