C++ 关于反序列化器的模板化专门化的含糊不清之处';s-pop函数

C++ 关于反序列化器的模板化专门化的含糊不清之处';s-pop函数,c++,C++,我有一个要反序列化的序列化对象队列。 我按照序列化对象的顺序反序列化对象。 对象可以是多种类型,包括字符串、结构和向量。 问题是,我正在寻找一种主流方法来弹出我的反序列化元素。 我不高兴不得不面对: int ex1 = deserializer.pop<int>(); box_t ex2 = deserializer.pop<box_t>(); std::string ex2 = deserializer.popString(); std::vector<float

我有一个要反序列化的序列化对象队列。 我按照序列化对象的顺序反序列化对象。 对象可以是多种类型,包括字符串、结构和向量。 问题是,我正在寻找一种主流方法来弹出我的反序列化元素。 我不高兴不得不面对:

int ex1 = deserializer.pop<int>();
box_t ex2 = deserializer.pop<box_t>();
std::string ex2 = deserializer.popString();
std::vector<float> ex3 = deserializer.popVector<float>();
intex1=反序列化程序.pop();
box_t ex2=反序列化程序.pop();
std::string ex2=反序列化程序.popString();
std::vector ex3=反序列化程序.popVector();
我想做

int ex1 = deserializer.pop<int>();
box_t ex2 = deserializer.pop<box_t>();
std::string ex3 = deserializer.pop<std::string>();
std::vector<float> ex4 = deserializer.pop<std::vector<float>>();
intex1=反序列化程序.pop();
box_t ex2=反序列化程序.pop();
std::string ex3=反序列化程序.pop();
std::vector ex4=反序列化程序.pop();
以下是我到目前为止的情况:

template<typename Test, template<typename...> class Ref>
struct is_specialization : std::false_type {};

template<template<typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref> : std::true_type {};

template<typename data_t>
data_t pop()
{
    if (is_specialization<data_t, std::vector>::value) {
        std::vector<issue1> data = deserialize_vector();
        return data; // issue2
    } else if ((is_specialization<data_t, std::string>::value)) {
        std::string data = deserialize_string();
        return data; // issue2
    } else {
        return deserialize<data_t>();
    }
}
模板
结构是_专门化:std::false_类型{};
模板
结构是_专门化:std::true_类型{};
模板
数据_t pop()
{
if(is_specialization::value){
std::vector data=反序列化_vector();
返回数据;//问题2
}else if((is_specialization::value)){
std::string data=反序列化_string();
返回数据;//问题2
}否则{
返回反序列化();
}
}
问题1:如何确定vector的数据类型? 问题2:编译器不会返回显式定义的类型。 甚至可以在一个功能中实现吗


谢谢您的时间。

这看起来是一个很好的标签分发用例。使用helper函数根据提供的类型重载,并为这些类型调用专门的
反序列化
函数。助手
类型\u t
标记类型可用于保存该类型,它还使界面更清晰

namespace impl{
模板结构类型\u t{using type=t;};
模板
T pop(T型){
返回反序列化();
}
模板
标准::矢量pop(t型){
返回反序列化_向量();
}
标准::字符串弹出(类型\u t){
返回反序列化字符串();
}
}
然后在您的外部
弹出窗口中

模板
数据_t pop(){
返回impl::pop(impl::type_t{});
}
如何确定vector的数据类型

您可以使用
typename data\u t::value\u type

编译器不会返回显式定义的类型。甚至可以在一个功能中实现吗

如果您可以访问C++17,只需通过
If constexpr
更改
If
(因为您的条件是编译时):

模板
数据_t pop()
{
if constexpr(is_specialization::value){
std::vector data=反序列化_vector();
返回数据;
}如果constexpr(is_specialization::value)为else{
std::string data=反序列化_string();
返回数据;
}否则{
返回反序列化();
}
}
在C++17之前(甚至使用C++17),标记分派是解决问题的一种好方法


SFINAE可能是另一种方法,但在您的案例中似乎更复杂。

非常有趣,我从未考虑过这样的范例。这正是我要找的!非常感谢。
template<typename data_t>
data_t pop()
{
    if constexpr (is_specialization<data_t, std::vector>::value) {
        std::vector<issue1> data = deserialize_vector();
        return data;
    } else if constexpr (is_specialization<data_t, std::string>::value) {
        std::string data = deserialize_string();
        return data;
    } else {
        return deserialize<data_t>();
    }
}