在 std::move 之后重复使用 std 容器是否安全?

Is it safe to reuse a std container after std::move?

例如,如果我有一个 std::list,并且我使用 std::move() 将其信息移动到 std::map,通过在列表中填充更多内容来重用该列表是否安全数据以便我可以向地图添加更多条目?我依稀记得另一位程序员告诉我,std::move() 会将事物置于未知状态,因此您以后不应该重用它们,但我只是想确认一下,因为我似乎无法找到任何关于此的信息四处寻找。

如果它不安全,那么在移动它之后在列表上调用 clear() 是否可以重复使用?

这是我正在做的一个例子:

// Create the initial list, give it some data, then move it into the map.
typedef std::list<std::pair<string, uint16>> TListType;
TListType data;
data.push_back(std::make_pair("some string", 0));
data.push_back(std::make_pair("some other string", 0));

std::map<uint32, TListType> mappedData;
mappedData.insert(std::make_pair(0, std::move(data)));

// Insert more data into the list, then move into the map as a new entry.
data.push_back(std::make_pair("new strings", 9));
mappedData.insert(std::make_pair(1, std::move(data)));

我必须做这些丑陋的事情的原因是因为我不能使用初始化列表而且我没有 Boost,所以我在如何初始化复杂的数据结构方面有点受限。

从标准 C++ 库 类 移出后,它们处于未指定状态,但在其他方面完全可用。相关部分是 17.6.5.15 [lib.types.movedfrom]:

Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.

None 个容器另有说明(据我所知)。

不过从声音上看,你实际上是在移动元素。然而,对于这些基于 MoveConstructibleMoveAssignable 含义的定义(来自第 17.6.3.1 节 [utility.arg.requirements];rv 是任何一个的参数移动构造或移动赋值):

rv’s state is unspecified [ Note:rv must still meet the requirements of the library component that is using it. The operations listed in those requirements must work as specified whether rv has been moved from or not. —end note ]