仅当元组中存在该类型时,将功能应用于元组元素
Applying a function to tuple element only if that type is present in tuple
我要实现的目标如下,以及我现在的位置:
template <typename Fn, typename Tuple, size_t... Is>
auto apply_if_impl(Tuple t, Fn&& f, std::index_sequence<Is...>) {
return std::make_tuple(
std::is_same_v<std::string, std::tuple_element_t<Is, Tuple>> ?
f(std::get<Is>(t)) :
std::get<Is>(t)...
);
}
template <typename Fn, typename ...Ts>
auto apply_if(std::tuple<Ts...> t, Fn&& f) {
return apply_if_impl(t, f, std::make_index_sequence<sizeof...(Ts)>());
}
并以:
的方式实现了这一点int main() {
std::tuple<int, std::string, float> t{42, "hello", 3.14f};
// this one should return
// std::tuple<int, std::size_t, float>{42, 5, 3.14f};
apply_if(t, [](std::string s){ return s.size(); });
// return value of this should be equal to t above since
// there is no vector element in the given tuple
apply_if(t, [](std::vector<int> s){ return s.size(); });
}
将返回另一个std::tuple
,但具有42和5(长度为 "hello"
(和3.14的std::tuple<int, std::size_t, float>
。如果给定的元组中没有可行的元素可以应用给定的可笑,只需返回给定的元组而无需做任何事情即可。因此,给定std::tuple<int, std::string, float>
的副本将在后一种情况下返回或移动。
我遇到的问题是,在我拥有的三元语句中,编译器仍然看到要应用于元组中其他成员的功能。我该如何解决?我需要一个编译时间三元物,可以正确扩展make_tuple
调用。最后,我还需要摆脱那个硬编码的std::string
。我需要在此处放置参数类型。
编辑:如果要使解决方案更容易,请随时使用boost::hana
之类的库。对我来说也是一个不错的练习。
您可以通过另一个中间模板进行:
template <bool>
class Select
{
public:
template <typename F, typename T>
T& operator()(F&, T& t) const
{
return t;
}
};
template <>
class Select<true>
{
public:
template <typename F, typename T>
auto operator()(F& f, T& t) const -> decltype(f(t))
{
return f(t);
}
};
template<typename Fn, typename Tuple, size_t ... Is>
auto apply_if_impl(Tuple t, Fn&& f, std::index_sequence<Is...>)
{
return std::make_tuple
(
Select<std::is_same_v<std::string, std::tuple_element_t<Is, Tuple>>>()
(f, std::get<Is>(t))...
);
}
c 17解决方案:
template<class Fn, typename Arg>
decltype(auto) apply_if_invocable(Fn fn, Arg&& arg) {
if constexpr (std::is_invocable_v<Fn, Arg>)
return fn(std::forward<Arg>(arg));
else
return std::forward<Arg>(arg);
}
template <typename Tuple, class Fn, size_t... Is>
auto apply_if_impl(Tuple&& t, Fn fn, std::index_sequence<Is...>) {
return std::make_tuple(apply_if_invocable(fn, std::get<Is>(std::forward<Tuple>(t)))...);
}
template <class Tuple, class Fn>
auto apply_if(Tuple&& t, Fn fn) {
return apply_if_impl(std::forward<Tuple>(t), fn,
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
,然后:
struct Fn {
int operator()(int v) {
return 0;
}
std::size_t operator()(const std::string& v) {
return v.length();
}
};
std::tuple<int, std::string, std::unique_ptr<int>> t{
42, "hello", std::make_unique<int>(21)};
auto z = apply_if(std::move(t), Fn{});
assert(std::get<0>(z) == 0);
assert(std::get<1>(z) == 5);
assert(*std::get<2>(z) == 21);
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 将元组的向量转换/构造为堆
- 专用于 std 元组的模板,而无需用户执行remove_cvref
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 类内部和外部静态 constexpr 元组之间的差异
- 可变参数模板与使用元组在参数中添加不同的数据对
- 如何部分专业化功能以用元组元素作为参数调用功能
- 仅当元组中存在该类型时,将功能应用于元组元素
- 从没有预处理器的HANA元组中创建功能签名
- C 元组类型用于功能参数类型
- 接受元组并返回另一个元组的功能
- 将功能指针及其参数作为推力::元组传递给全局函数