按类型从元组中获取值(而不是索引)
从 C++11 开始,没有 STL 方法可以获取 type 元组的第一个元素T
。
在 C++14 中,应该有一种方法使用新的重载std::get
来做你想做的事。ISO 文件位于此处N3404和此处N3670。
您可以在 C++11 中使用以下命令执行此操作:
#include<tuple>
#include<type_traits>
#include<string>
#include<iostream>
template<int Index, class Search, class First, class... Types>
struct get_internal
{
typedef typename get_internal<Index + 1, Search, Types...>::type type;
static constexpr int index = Index;
};
template<int Index, class Search, class... Types>
struct get_internal<Index, Search, Search, Types...>
{
typedef get_internal type;
static constexpr int index = Index;
};
template<class T, class... Types>
T get(std::tuple<Types...> tuple)
{
return std::get<get_internal<0,T,Types...>::type::index>(tuple);
}
我把它托管在 Ideone here上,但这是我的后代测试功能
int main()
{
std::tuple<int, double, std::string> test{1, 1.7, "test"};
std::cout<<"get<0> == get<int> :"<< (std::get<0>(test) == get<int>(test))<< "\n";
std::cout<<"get<1> == get<double> :"<<(std::get<1>(test) == get<double>(test))<< "\n";
std::cout<<"get<2> == get<std::string> :"<<(std::get<2>(test) == get<std::string>(test))<< "\n";
}
基于@Yakk 扩展它以支持类型的多个实例以及在元组中测试的谓词的想法,他提供了以下代码(也托管在 Ideone here上)
被警告:在 C++14 中,新的重载ofstd::get
不允许元组中有多个相同类型的实例。相反,它会发出编译错误。此外,C++14 版本也不支持谓词。
//Include same headers as before
template<bool b, typename T=void>
using EnableIf = typename std::enable_if<b,T>::type;
template<int Index, template<typename T>class Search, int Which, typename, class First, class... Types>
struct get_internal:
get_internal<Index + 1, Search, Which, void, Types...>
{};
template<int Index, template<typename T>class Search, int Which, class First, class... Types>
struct get_internal<Index, Search, Which, EnableIf<!Search<First>::value>, First, Types...>:
get_internal<Index + 1, Search, Which, void, Types...>
{};
template<int Index, template<typename T>class Search, int Which, class First, class... Types>
struct get_internal<Index, Search, Which, EnableIf<Search<First>::value>, First, Types...>:
get_internal<Index + 1, Search, Which-1, void, Types...>
{};
template<int Index, template<typename T>class Search, class First, class... Types>
struct get_internal<Index, Search, 0, EnableIf<Search<First>::value>, First, Types...>:
std::integral_constant<int, Index>
{};
template<template<typename>class Test, int Which=0, class... Types>
auto get(std::tuple<Types...>& tuple)->
decltype(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple))
{
return std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple);
}
template<template<typename>class Test, int Which=0, class... Types>
auto get(std::tuple<Types...> const& tuple)->
decltype(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple))
{
return std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple);
}
template<template<typename>class Test, int Which=0, class... Types>
auto get(std::tuple<Types...>&& tuple)->
decltype(std::move(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple)))
{
return std::move(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple));
}
template<typename T>
struct is_type {
template<typename U>
using test = std::is_same<T,U>;
};
template<class T, int Which=0, class... Types>
T& get(std::tuple<Types...>& tuple)
{
return get<is_type<T>::template test,Which>(tuple);
}
template<class T, int Which=0, class... Types>
T const& get(std::tuple<Types...> const& tuple)
{
return get<is_type<T>::template test,Which>(tuple);
}
template<class T, int Which=0, class... Types>
T&& get(std::tuple<Types...>&& tuple)
{
return std::move(get<is_type<T>::template test,Which>(tuple));
}
获取元组中第 n 个元素的类型
有一种方法可以获取第 n 个元素的类型。std::tuple_element<n, decltype(tuple)>::type
(感谢@syam)是元组的第 n 个元素的类型。