私はこのコードを持っています:
...
#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
問題は、タプル全体の潜在的に高価なコピーであるのに対し、 usingtie
は参照のタプル (はるかに小さい) であるため、このソリューションは非効率的であるということです。it->first
では、どうすればタプルへの参照を取得できますか? 同じtie
トリックを使用する必要がありますか?