c++ - 如何转发元组的类型以专门化其他模板?
问题描述
目前我正在研究一种动态容器结构,它代表一个 pod 值或具有相同容器类型的指针向量。容器有一个接口optional<T> expect_value<T>()
。对于 pod 类型,实现很简单。对于非 pod 值,我会调用expect_value<tuple<args...>>()
,args
也将是元组。但是在实现这个功能时,我遇到了一个麻烦:如何重定向a.expect_value<tuple<args...>>()
到a.expect_value_tuple<args...>>()
. 例如,调用a.expect_value<tuple<int,int>()
将返回a.expect_value_tuple<int, int>()
. 因为参数是空的,我不能使用解压参数的类型推导。然后整个项目就无法再进行了。有任何想法吗?下面是我的意图的最小示例。
#include <tuple>
#include <vector>
#include <optional>
#include <functional>
using namespace std;
template<typename T>
struct is_tuple_impl : std::false_type {};
template<typename... Ts>
struct is_tuple_impl<std::tuple<Ts...>> : std::true_type {};
template<typename T>
struct is_tuple : is_tuple_impl<std::decay_t<T>> {};
class my_container;
template<typename... args, size_t... arg_idx>
optional<tuple<args>...> get_tuple_value_from_vector(const vector<my_container*>& v_list, std::index_sequence<arg_idx...>)
{
auto temp_result = make_tuple((*v_list[arg_idx]).expect_value<arg>()...);
if(!(get<arg_idx>(temp_result) &&...))
{
return nullopt;
}
return make_tuple(get<arg_idx>(temp_result).value()...);
}
class my_container
{
public:
int value_type; // 1 for v_int 2 for v_list 0 empty
union
{
int v_int;
};
vector<my_container*> v_list;
template<typename T>
optional<T> expect_simple_value();
template<typename... args>
optional<tuple<args...>> expect_tuple_value();
template<typename T>
optional<T> expect_value();
};
template <typename T>
optional<T> my_container::expect_simple_value()
{
return nullopt;
}
template <>
optional<int> my_container::expect_simple_value()
{
if(value_type == 1)
{
return v_int;
}
return nullopt;
}
template<typename... args>
optional<tuple<args...>> my_container::expect_tuple_value()
{
if(v_list.size() == 0)
{
return nullopt;
}
for(const auto i: v_list)
{
if(!i)
{
return nullopt;
}
}
auto the_tuple_size = sizeof...(args);
if(v_list.size() != the_tuple_size)
{
return nullopt;
}
return get_tuple_value_from_vector<args...>(v_list, index_sequence_for<args...>{});
}
template <typename T>
optional<T> my_container::expect_value()
{
if(is_tuple<T>::value)
{
return expect_tuple_value<T>();
}
else
{
return expect_simple_value<T>();
}
}
int main()
{
my_container test_value;
test_value.value_type = 1;
test_value.v_int = 1;
auto result = test_value.expect_value<tuple<int, int>>();
if(result)
{
return 0;
}
else
{
return 1;
}
}
问题的核心是线 return expect_tuple_value<T>();
当逻辑去那里时, T 应该是 tuple<args...>
,但我想要的是 return return expect_tuple_value<args...>()
。
解决方案
如何通过函数模板的部分排序来使用模板参数推导和重载解析:
class my_container
{
public:
template<class T> optional<T> expect_value_simple();
template<class...Args> optional<tuple<Args...>> expect_value_tuple();
private:
template<class T> struct deduce_type{};
template<typename T>
auto expect_value_dispatching(deduce_type<T>){
return expect_value_simple<T>();
}
template<typename...Args>
auto expect_value_dispatching(deduce_type<tuple<Args...>>){
return expect_value_tuple<Args...>();
}
public:
template<typename T>
auto expect_value(){
return expect_value_dispatching(deduce_type<T>{});
}
};
(演示)
推荐阅读
- python - pygame.error:库未初始化,虽然已经初始化
- compilation - Fortran 共享库 .so 显示未定义符号错误
- python - 如果不在 pyspark 中,则保持相同 ID 或第一行的两列中的值匹配
- typescript - 如何使用 Next.js 中的自定义 webpack 配置将“cmd”提供给 Sentry(@sentry/nextjs 包)的浏览器?
- sql - 敏捷工具包 atk4 中的左连接和非 id 连接
- django - Django 服务器返回身份验证错误
- blazor - 在服务器端 Blazor 中登录后重定向
- android - 硬件后退按钮被调用两次
- pine-script - pinescript回测不同时间
- google-sheets - 当将数据从一张纸的单元格传输到另一张纸时,我得到#ref!错误。我该如何解决?