【发布时间】:2020-04-10 02:44:36
【问题描述】:
我实现了迭代器,我想设置比较函数来设置序列的边界。 所以在构造函数中我接受了这个参数
iterator(std::pair<T, std::function<T(T&)>> rhs, const std::function<bool(T&, T&)> cmp):
base(rhs), cmp(cmp), gen(range<T>::gen_function(rhs)) {}
next... 我尝试在 operator== 中使用 cmp 仿函数:
bool operator==(const iterator& rhs) const { return cmp(**this, *rhs); }
bool operator!=(const iterator& rhs) const { return !cmp(**this, *rhs); }
clang++ 编译(v 11.0.0,Ubuntu 18)
clang++ -std=c++2a -stdlib=libc++ coroutins_iterator.cpp -v
编译器给我错误:
./coroutins_iterator.h:52:56: error: no matching function for call to object of type 'const std::function<bool (int &, int &)>'
bool operator!=(const iterator& rhs) const { return !cmp(**this, *rhs); }
^~~
coroutins_iterator.cpp:31:14: note: in instantiation of member function 'range<int>::iterator::operator!=' requested here
for (auto a : range<int>(2, 10, [](int &a){ return a * 2; })) {
^
/usr/include/c++/v1/functional:2342:9: note: candidate function not viable: expects an l-value for 1st argument
_Rp operator()(_ArgTypes...) const;
如何解决?
或者..我可以用另一种方式制作那个界面吗?
【问题讨论】:
-
您的
operator*()方法是否返回引用? -
@0x499602D2, no, "T operator*() const { return gen.current_num(); }"
-
好的,把你的
function签名改成std::function<bool(T const&, T const&)>。
标签: c++ compiler-errors clang clang++ std-function