gpt4 book ai didi

c++ - std::tuple 的笛卡尔积

转载 作者:行者123 更新时间:2023-12-02 16:02:06 24 4
gpt4 key购买 nike

为了对严重依赖模板的 C++17 框架进行单元测试,我尝试编写帮助程序 template生成两组数据类型的笛卡尔积的类,由两个元组给出:

**Input**: std::tuple <A, B> std::tuple<C,D,E>

**Expected output**: Cartesian product of the two tuples:
std::tuple<std::tuple<A,C>, std::tuple<A,D>, std::tuple<A,E>,
std::tuple<B,C>, std::tuple<B,D>, std::tuple<B,E>>

我知道 Boost MP11 offers such features但我不想为了测试目的而包含对另一个库的依赖。到目前为止,我想出了一个非常直接的解决方案,虽然要求类是默认构造的(Try it here!):

template <typename T1, typename T2,
typename std::enable_if_t<is_tuple_v<T1>>* = nullptr,
typename std::enable_if_t<is_tuple_v<T2>>* = nullptr>
class CartesianProduct {
protected:
CartesianProduct() = delete;
CartesianProduct(CartesianProduct const&) = delete;
CartesianProduct(CartesianProduct&&) = delete;
CartesianProduct& operator=(CartesianProduct const&) = delete;
CartesianProduct& operator=(CartesianProduct&&) = delete;

template <typename T, typename... Ts,
typename std::enable_if_t<std::is_default_constructible_v<T>>* = nullptr,
typename std::enable_if_t<(std::is_default_constructible_v<Ts> && ...)>* = nullptr>
static constexpr auto innerHelper(T, std::tuple<Ts...>) noexcept {
return std::make_tuple(std::make_tuple(T{}, Ts{}) ...);
}
template <typename... Ts, typename T,
typename std::enable_if_t<std::is_default_constructible_v<T>>* = nullptr,
typename std::enable_if_t<(std::is_default_constructible_v<Ts> && ...)>* = nullptr>
static constexpr auto outerHelper(std::tuple<Ts...>, T) noexcept {
return std::tuple_cat(innerHelper(Ts{}, T{}) ...);
}
public:
using type = std::decay_t<decltype(outerHelper(std::declval<T1>(), std::declval<T2>()))>;
};
template <typename T1, typename T2>
using CartesianProduct_t = typename CartesianProduct<T1, T2>::type;

同样,当尝试以类似的方式(try it here)实例化模板类列表时,我必须做出相同的假设:我不能将它应用于具有 protected 的类。/private构造函数(没有 friend 声明)并且不可默认构造。

是否有可能解除默认可构造性的限制而不求助于 std::integer_sequence 和一个额外的助手类?据我了解,不可能使用 std::declval<T>() 直接在方法中 innerHelperouterHelper (这将解决我的问题),因为它似乎不是 unevaluated expression了。至少GCC complains then about static assertion failed: declval() must not be used! 而它seems to compile fine with Clang .

提前致谢!

最佳答案

解决方法之一是省略函数定义并直接使用decltype 来推断返回类型:

template<typename T1, typename T2>
class CartesianProduct {
template<typename T, typename... Ts>
static auto innerHelper(T&&, std::tuple<Ts...>&&)
-> decltype(
std::make_tuple(
std::make_tuple(std::declval<T>(), std::declval<Ts>())...));

template <typename... Ts, typename T>
static auto outerHelper(std::tuple<Ts...>&&, T&&)
-> decltype(
std::tuple_cat(innerHelper(std::declval<Ts>(), std::declval<T>())...));

public:
using type = decltype(outerHelper(std::declval<T1>(), std::declval<T2>()));
};

class Example {
Example() = delete;
Example(const Example&) = delete;
};

using T1 = std::tuple<Example>;
using T2 = std::tuple<int, double>;
static_assert(
std::is_same_v<
CartesianProduct_t<T1, T2>,
std::tuple<std::tuple<Example, int>, std::tuple<Example, double>>>);

Demo.

关于c++ - std::tuple 的笛卡尔积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70404549/

24 4 0
文章推荐: c# - 如何将C# Winform Project连接到局域网中的XAMPP MySQL服务器?
文章推荐: typescript - 有没有办法提取扩展类型的类型?
文章推荐: arrays - 如何使用泛型和特征从数组中获取最大的元素?
文章推荐: html - 我正在尝试使用 CSS 样式显示 : inline-block; with 2
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com