Insert list-initialized pair into std :: map

I am trying to insert a move type only into a map. I have the following code:

#include <map>

class Moveable
{
public:
    Moveable() = default;
    Moveable(const Moveable&) = delete;
    Moveable(Moveable&&) = default;
    Moveable& operator=(const Moveable&) = delete;
    Moveable& operator=(Moveable&&) = default;
};

int main() {
    std::map<int,Moveable> my_map;
    Moveable my_moveable_1, my_moveable_2, my_moveable_3;
    my_map.insert(std::pair<int,Moveable>{1, std::move(my_moveable_1)}); // (1)
    my_map.insert(std::make_pair(2, std::move(my_moveable_2)));          // (2)
    my_map.insert({3, std::move(my_moveable_3)});                        // (3)

    return 0;
}

What happens is that VisualC ++ compiles using lines 1, 2, and 3. In clang and gcc, only compilation 1 and 2 and line 3 give an error (using the remote copy constructor).

Question: Which compiler is right and why?

Try it here: rextester

+4
source share
2 answers

std::map::insert has (among other things) the following overloads:

// 1.
std::pair<iterator,bool> insert( const value_type& value );

// 2. (since C++11)
template< class P >
std::pair<iterator,bool> insert( P&& value );

// 3. (since C++17)
std::pair<iterator,bool> insert( value_type&& value );

, , . , ++ 11, , ( , ++ 11, ).

insert ++ 11 , , .

++ 17 , . , , , , , ++ 17 .

UPDATE: , : ( ), insert({k,v}), insert(pair<K,V>{k,v}). , ++ 11.

+1

g++ 7.3 !

clang++ 5.0.1 .

, ++ 20, .

0

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


All Articles