std :: map <> :: insert mit nicht kopierbaren Objekten und einheitlicher Initialisierung
Schauen Sie sich den folgenden Code an:
#include <utility>
#include <map>
// non-copyable but movable
struct non_copyable {
non_copyable() = default;
non_copyable(non_copyable&&) = default;
non_copyable& operator=(non_copyable&&) = default;
// you shall not copy
non_copyable(const non_copyable&) = delete;
non_copyable& operator=(const non_copyable&) = delete;
};
int main() {
std::map<int, non_copyable> map;
//map.insert({ 1, non_copyable() }); < FAILS
map.insert(std::make_pair(1, non_copyable()));
// ^ same and works
}
Das Kompilieren dieses Snippets schlägt fehl, wenn die markierte Zeile in g ++ 4.7 aus dem Kommentar entfernt wird. Der erzeugte Fehler weist darauf hinnon_copyable
kann nicht kopiert werden, aber ich habe damit gerechnet, dass es verschoben wird.
Warum fügt man einstd::pair
mit einheitlicher Initialisierung erstellt, aber keine mitstd::make_pair
? Sollen nicht beide Werte erzeugen, die erfolgreich in die Karte verschoben werden können?