I try to directly consume elements from a set, which i cannot get to work from the outside getting an error
binding reference of type ‘int&&’ to ‘std::remove_reference<const int&>::type’ {aka ‘const int’} discards qualifiers
Consuming from a vector works perfectly fine. I really do not understand where syntactic the difference would be between consuming from the set or the vector, as they work similar cocerning their iterators.
I can make a Workaround pops()
, however, I do not neccessarily see this as a intuitive solution.
#include <vector>
#include <set>
#include <iostream>
class A {
public:
std::vector<int> intv_{1,2,3};
std::set<int> ints_{1,2,3};
int pops() {
auto rslt = std::move(*ints_.begin());
ints_.erase(ints_.begin());
return rslt;
}
};
using namespace std;
void consume_int(bool dummy, int&& i) {
cout << i << endl;
}
using namespace std;
int main() {
A a;
consume_int(true, std::move( *(a.intv_.begin()) )); //OK!
a.intv_.erase(a.intv_.begin());
consume_int(true, std::move( *a.ints_.begin() )); //FAIL to compile
a.ints_.erase(a.ints_.begin());
consume_int(true, std::move(a.pops())); //Workaround OK!
return 0;
}