std::map with non-copyable key type?

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
> The above code compiles fine under Visual Studio 2017
> but it fails using a lengthy error message under GCC/G++ 5.4.0 ...

It compiles cleanly with the current release of the GNU compiler (7.1)
http://coliru.stacked-crooked.com/a/3262da7c2623bdae

Version 5 is the first version from GNU which attempted to provide a conforming C++11 standard library; it is bound to have some rough edges (the code base is messy because of they thought that it would be politically correct to provide backward compatibility with the thoroughly broken implementations of the library in versions prior to 5.1).
Topic archived. No new replies allowed.