没有 == 为提升::元组定义

no == defined for boost::tuples

本文关键字:元组 定义 没有      更新时间:2023-10-16

我有这个代码:

...
#include "boost/tuple/tuple_comparison.hpp"
...
template <typename ReturnType, typename... Args>
function<ReturnType(Args...)> memoize(const Args && ... args)
{
    using noRef = boost::tuple<typename std::remove_reference<Args>::type...>;
    static map<noRef, ReturnType, less<>> cache;
    auto key = std::tie(noRef{ boost::make_tuple(args ...) });
    auto it = cache.lower_bound(key);
    ReturnType result;
    if (it->first == key) { ...

但是当我尝试编译它时,我收到此错误:

error C2678: binary '==': no operator found which takes a left-hand operand of type 'const noRef' (or there is no acceptable conversion)

为什么会发生这种情况,因为noRefboost::tuple的别名,tuple_comparison应该管理这种情况?

发现错误,不知道如何解决:

似乎错误出在std::tie操作中。所以把它改写为:

    auto key = noRef{ boost::make_tuple(args ...) };

工作正常。问题是这个解决方案效率低下,因为key是整个元组的潜在昂贵副本,而使用 tie 是引用元组(小得多(。那么,我怎样才能引用it->first元组呢?我应该使用相同的tie技巧吗?

编译此行的唯一原因是 MSVC 的 Evil ExtensionTM,它允许非常量左值引用绑定到临时引用:

auto key = std::tie(noRef{ boost::make_tuple(args ...) });

这应该只是

auto key = boost::tie(args...);

这将创建供以后查找的引用boost::tuple

此外,如评论中所述,if检查应先验证it != cache.end(),然后再尝试取消引用它(谢谢!

最后,const Args && ...没有多大意义,因为人们不太可能想要接受常量右值。它可能应该是const Args&...Args&&....