Error copying objects when class contains boost :: container :: flat_set

Under the (false?) Impression that boost::container::flat_set was a replacement for std::set , I replaced set with flat_set where I expected the number of elements to be small and search performance more critical than inserts.

At a later stage, I was alerted by a confusing compilation error, which I eventually traced back to using flat_set as a member of the class.

For instance:

 class Room { private: boost::container::flat_set<int> v; }; 

The following code will not compile, but works 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 

I see a compilation error:

  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:

  • Is this error expected? If so, how do I get around it?
  • How do the three operators in the code example differ from each other, and why only the last occurs?
  • Why does the compiler complain about the assignment operator Room , and not about flat_set ? Has flat_set used for the default operators generated for the class?

Full 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; }; 
+4
source share
2 answers

This is a well-known motion emulation restriction implemented by Boost.Move. You can find more information about this here: http://www.boost.org/doc/libs/1_52_0/doc/html/move/emulation_limitations.html#move.emulation_limitations.assignment_operator

+3
source

Disclaimer: I - OP; I accepted K-ballo's answer as it led to a final decision, and I am posting it to supplement this stream with some details.

As mentioned in the accepted answer, this is a really well-known restriction for classes that use the BOOST_COPYABLE_AND_MOVABLE macro (which applies to many classes in boost::container , including flat_set and flat_map ).

To overcome this, I defined an assignment operator for a class that takes a const argument argument. For example, in the case of a sample Room class in quesiton, it will be line by line:

 class Room { private: boost::container::flat_set<int> v; public: Room& operator=(const Room& source) { v = source.v; return *this; } }; 
+1
source

Source: https://habr.com/ru/post/1445804/


All Articles