Under the (false?) impression that boost::container::flat_set was a drop-in replacement of std::set, I replaced set with flat_set wherever I expected the number of element to be small and the search performance is more critical than inserts.
At a later stage, I was stumped by a confusing compilation error which I eventually traced to the use of flat_set as a class member.
For example:
class Room {
private:
boost::container::flat_set<int> v;
};
The following code will not compile, but works just fine if I replace flat_set with std::set.
Room a;
Room b = Room(); // Example 1. Compiles OK
a = b; // Example 2. Compiles OK
a = Room(); // Example 3. Eeeek! Compile fails on this line
The compilation error I see is:
error: no match for ‘operator=’ in ‘a = Room()’ note: candidate is: note: Room& Room::operator=(Room&) note: no known conversion for argument 1 from ‘Room’ to ‘Room&’
My questions are:
- Is this error expected? If it is, then how do I work round it?
- How are the three statements in the example code different, and why does only the last one fail?
- Why does the compiler complain about the assignment operator of
Roomrather thanflat_set? Has the use offlat_setinfluenced the default operators generated for the class?
Complete sample program:
#include <boost/container/flat_set.hpp>
class Room {
private:
boost::container::flat_set<int> v;
};
int main(int argc, char *argv[]) {
Room a;
Room b = Room();
a = b;
a = Room(); // compilation fails here
return 0;
};
This is a known limitation of the move emulation performed by Boost.Move. You can find more information about it here: http://www.boost.org/doc/libs/1_52_0/doc/html/move/emulation_limitations.html#move.emulation_limitations.assignment_operator