我使用perform_mapping
from link作为here link,但出现编译错误。
#include <string>
#include <tuple>
#include <utility>
#include <string>
#include <type_traits>
namespace details
{
template <typename Tuple, typename Mapping>
struct return_type;
template <template <typename ...> typename Tuple, typename ... Types, typename Mapping>
struct return_type<Tuple<Types...>, Mapping>
{
//I changed the below line from what is in the link
using type = Tuple<decltype(std::invoke_result<Mapping, Types>())...>;
};
template <template <typename, std::size_t> typename Array, typename T, std::size_t Size, typename Mapping>
struct return_type<Array<T, Size>, Mapping>
{
using type = Array<std::invoke_result_t<Mapping, T>, Size>;
};
template <typename Tuple, typename Mapping>
using return_type_t = typename return_type<Tuple, Mapping>::type;
template <typename Tuple, typename Mapping, std::size_t ... Indices>
return_type_t<std::decay_t<Tuple>, std::decay_t<Mapping>> perform_mapping(Tuple&& tup, Mapping&& mapping, std::index_sequence<Indices...>)
{
return {mapping(std::get<Indices>(std::forward<Tuple>(tup)))...};
}
}
template <typename Tuple, typename Mapping,
std::size_t Size = std::tuple_size<std::decay_t<Tuple>>::value>
auto perform_mapping(Tuple&& tup, Mapping&& mapping)
{
return details::perform_mapping(std::forward<Tuple>(tup), std::forward<Mapping>(mapping), std::make_index_sequence<Size>{});
}
struct A
{
A(double z) : x(z){};
double x;
using Type = double;
};
struct B
{
B(std::string s) : x(std::move(s)) {}
std::string x;
using Type = std::string;
};
struct C
{
C() : m_tuple({A(1.0), B("A")})
{
}
template<class T>
typename T::Type f(T& z)
{
return z.x;
}
std::tuple<A::Type, B::Type> get()
{
return perform_mapping(m_tuple, [this](auto& z) { return this->f(z); });
}
std::tuple<A, B> m_tuple;
};
int main()
{
C c;
auto t = c.get();
};
我的问题是:我可以perform_mapping
按照上面的代码的方式实现吗?
如果您可以使用 C++17,您可以从 中受益
std::apply
,它与可变参数 lambda 一起是“解包”元组的非常方便的工具:(完美转发留作练习。)