Aug 14, 2017 at 1:14pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <map>
struct movable_key_type
{
int key_thing;
inline movable_key_type( void ) {}
inline movable_key_type( movable_key_type&& right ) { this ->key_thing = right.key_thing; }
inline movable_key_type( const movable_key_type& right ) = delete ;
inline movable_key_type& operator = ( movable_key_type&& right )
{
this ->~movable_key_type();
return *new (this ) movable_key_type( std::move( right ) );
}
inline movable_key_type& operator = ( const movable_key_type& right ) = delete ;
inline bool operator < ( const movable_key_type& right ) const
{
return ( this ->key_thing < right.key_thing );
}
};
int main( int argc, char *argv[] )
{
std::map <movable_key_type, int > test_map;
std::map <movable_key_type, int > test_map_2;
test_map = std::move( test_map_2 );
return 0;
}
The above code compiles fine under Visual Studio 2017 but it fails using a lengthy error message under GCC/G++ 5.4.0 ...
/usr/include/c++/5/ext/new_allocator.h:120:4: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = const movable_key_type; _T2 = int ]’
Problem is that the key type of std::map does not permit copy operation.
Having meta-data that is not copyable in the key type is pretty useful.
If you comment out the std::move of both maps, then no error. Wtf?
Please help!
Last edited on Aug 14, 2017 at 1:29pm UTC