如何包装(撰写)增强 hana 地图并访问括号运算符(运算符 [])?

How to wrap (compose) a boost hana map and access the bracket operator (operator[])?

本文关键字:运算符 访问 hana 何包装 包装 撰写 增强 地图      更新时间:2023-10-16

我正在使用hana地图(使用hana::make_map创建(。我有一个非常简单的类,它将继承自 hana 地图,并公开第二个地图


auto values = hana::make_map( ... );
auto uncertainties = hana::make_map( ... );
template< typename Values, typename Uncertainty >
struct map: Values{
Uncertainty uncertainty;
constexpr map(Values v, Uncertainty u ):
Values( v ),
uncertainty( u )
{ }
};
auto data = map( values, uncertainties );
// I want to do the following
auto hbar = data[ hbar ]; // Type hbar defined elsewhere
auto hbar_u = data.uncertainty[ hbar ]

这曾经有效。我最近更新了我们的boost hana版本,现在我收到以下编译器错误:

map.hpp:2:13: error: base
'map_impl' is marked 'final'
struct map: Values{
^

如果我正确理解了此消息,则已明确标记了boost hana,因此我无法再继承。

我真正想做的是使用operator[]访问值图,并使用.uncertainty访问不确定性图。我该怎么做?

我真的不想使用任何其他 boost 库;hana 对于我的项目来说绰绰有余。

通常首选使用聚合与继承。

正如"Mooing Duck"在评论中所说,您可以使用一个功能转发values地图的operator[]

查看 Boost.Hana 中operator[]的实现,您会注意到每个值类型都有一个重载,因为无法推断this。(他们应该解决这个问题(

如果需要,将values强制转换为适当的引用类型将允许您返回正确值类型的引用。这将使它的行为与hana::map完全相同。

#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL 1
#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;
template <typename Values, typename Uncertainty>
struct my_map_t {
Values values;
Uncertainty uncertainty;

constexpr decltype(auto) operator[](auto&& key) & {
return static_cast<Values&>(values)[
std::forward<decltype(key)>(key)];
}
constexpr decltype(auto) operator[](auto&& key) && {
return static_cast<Values&&>(values)[
std::forward<decltype(key)>(key)];
}
constexpr decltype(auto) operator[](auto&& key) const& {
return static_cast<Values const&>(values)[
std::forward<decltype(key)>(key)];
}
};
// Note: Clang 10 does not provide implicit deduction guides yet
constexpr auto values = hana::make_map(
hana::make_pair("foo"_s, 42) 
);
constexpr auto uncertainties = hana::make_map(
hana::make_pair("bar"_s, 5)
);

constexpr auto my_map = my_map_t(values, uncertainties);
static_assert(my_map["foo"_s] == 42);
static_assert(my_map.uncertainty["bar"_s] == 5);
int main() { }

https://godbolt.org/z/XVNsy-


如果你想使用无聊的旧C++17:

https://godbolt.org/z/i-NZd6