我想在包含原子的地圖中構造一個對象,因此它既不能被複制也不能移動AFAICT。在地圖中構建一個不可移動的對象
我讀的C++ reference是地圖emplace
應該能夠做到這一點。但是下面的代碼因爲刪除或不存在的構造函數而不能編譯。使用make_pair
沒有幫助。
#include <atomic>
#include <unordered_map>
class Z {
std::atomic<int> i;
};
std::unordered_map<int, Z> map;
void test(void) {
map.emplace(0, Z()); // error
map[0] = Z(); // error
}
這是可能的,如果沒有,爲什麼不呢?
編輯:編譯器是gcc 4.8.1,在Linux上
你用什麼編譯器? – Drop
'emplace'採用構造函數參數,但傳遞一個臨時對象。 –
@NeilKirk那麼如何調用no-arg構造函數呢? –