我正试图绕过伟大的提升:: hana,并试图弄清楚如何翻译一些旧的学校元组代码。
也许它有点特别,但我的类型既不可复制也不可移动,并且想要将它们打包在容器中。想一想政策设计......
我想我不知道如何在价值世界中创造一个hana元组。我正在尝试的方式不起作用,因为make<Tuple>(CompT{}...)
需要可复制或至少可移动的类型。
所以很可能这不是这样做的方法。或者这是hana的限制吗?
谢谢!
struct NonMoveable
{
NonMoveable() = default;
NonMoveable(const NonMoveable& other) = delete;
int val;
};
struct Simple { int val; };
template <typename ...CompT>
struct OldSchoolContainer
{
template <int I>
auto& getComponent()
{
return std::get<I>(components);
}
std::tuple<CompT...> components;
};
template <typename ...CompT>
struct HanaContainer
{
using Args_t = decltype(make<Tuple>(CompT{}...));
template <int I>
auto& getComponent()
{
return components[int_<I>];
}
Args_t components;
};
int main()
{
OldSchoolContainer<Simple> simpleOsc;
static_assert(std::is_same<Simple&, decltype(simpleOsc.getComponent<0>())>::value, "");
HanaContainer<Simple> simpleHanaC;
static_assert(std::is_same<Simple&, decltype(simpleHanaC.getComponent<0>())>::value, "");
OldSchoolContainer<NonMoveable> nonmoveableOsc;
static_assert(std::is_same<NonMoveable&, decltype(nonmoveableOsc.getComponent<0>())>::value, "");
// !!! this does not compile because hana's tuple closure needs to either move or copy
HanaContainer<NonMoveable> nonmoveableHanaC;
static_assert(std::is_same<NonMoveable&, decltype(nonmoveableHanaC.getComponent<0>())>::value, "");
return 0;
}
答案 0 :(得分:2)
您可以使用与hana::_tuple<...>
几乎完全相同的std::tuple
类型。
因此,以下工作:
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/tuple.hpp>
#include <tuple>
#include <type_traits>
namespace hana = boost::hana;
struct NonMoveable
{
NonMoveable() = default;
NonMoveable(const NonMoveable& other) = delete;
int val;
};
struct Simple { int val; };
template <typename ...CompT>
struct HanaContainer
{
template <int I>
auto& getComponent()
{
return components[hana::int_<I>];
}
hana::_tuple<CompT...> components;
};
int main()
{
HanaContainer<Simple> simpleHanaC;
static_assert(std::is_same<Simple&, decltype(simpleHanaC.getComponent<0>())>::value, "");
HanaContainer<NonMoveable> nonmoveableHanaC;
static_assert(std::is_same<NonMoveable&, decltype(nonmoveableHanaC.getComponent<0>())>::value, "");
return 0;
}