没有 == 为提升::元组定义
no == defined for boost::tuples
我有这个代码:
...
#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)
为什么会发生这种情况,因为noRef
是boost::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&&...
.
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- std::ranges::elements_view,用于自定义类似元组的数据
- 如何从参数包定义值类型的元组
- 元组 std::get() 不适用于变量定义的常量
- 元组的定义和初始化,其组件属于同一模板化类,但具有不同的专用化
- 使用GCC v4.8在Ubuntu 14.04上定义C 11中元组向量时的编译错误
- 如何类型定义作为其他元组串联的元组
- 推力:redy_by_key将zip_iterator(元组)传递到自定义函子中,以通过键检索平均值
- 如何使用 C++11 可变参数模板来定义由向量元组支持的元组向量
- 没有 == 为提升::元组定义
- 比较具有替代排序的自定义类型的std::元组(或std::对).是否可以插入自定义的小于/比较函数
- 无法重载元组的模板运算符,但自定义类型正常
- 删除用第一个指针和元素数定义的uint8_t数组
- 为不使用元组的自定义映射模拟std::map迭代器
- 如何通过lambda使用自定义相等来查找在std::vector中具有一个匹配元素的元组
- 使用gtest和google sparsehash时重新定义元组
- c++元组链接问题:未定义的引用
- 自定义对元组向量进行排序
- 自定义排序元组向量时出错