C++11 g++;4.9.2和右值参考图
因此,我有以下代码:C++11 g++;4.9.2和右值参考图,c++11,rvalue-reference,C++11,Rvalue Reference,因此,我有以下代码: #include <map> #include <string> #include <iostream> struct MockMapItem { std::string property; MockMapItem(const std::string& value) : property(value) { std::cout << "constructed MockMapItem w
#include <map>
#include <string>
#include <iostream>
struct MockMapItem {
std::string property;
MockMapItem(const std::string& value) : property(value) {
std::cout << "constructed MockMapItem with a string!" << std::endl;
}
};
typedef typename std::map<std::string, const MockMapItem&& > ItemMap;
int main()
{
MockMapItem map_item("some string value");
ItemMap themap;
themap.emplace("something", std::move(map_item));
}
#包括
#包括
#包括
结构MockMapItem{
std::字符串属性;
MockMapItem(常量std::字符串和值):属性(值){
std::cout23.2.1规定容器的值类型
必须可从容器中擦除,23.2.4规定对于映射和多重映射,这些要求适用于键类型
和映射类型
引用类型不能从容器中擦除,因为您无法形成指向引用类型的指针,也无法使用分配器\u traits::destroy
(因为分配器仅支持非常量对象类型,而引用类型不是对象类型)
因此,const MockMapItem&
类型无法满足在容器中使用的要求,因此具有未定义的行为
编辑:实际上,在C++11中,23.2.1中的要求只是值类型
是可破坏的,而引用类型是可破坏的。因此,在C++11中,这可能是格式良好的,但在C++14.Hmm中不是
Compilation error time: 0 memory: 0 signal:0
prog.cpp:14:60: error: template argument 2 is invalid
typedef typename std::map<std::string, const MockMapItem&& > ItemMap;
^
prog.cpp:14:60: error: template argument 4 is invalid
prog.cpp:14:62: error: 'ItemMap' in namespace 'std' does not name a type
typedef typename std::map<std::string, const MockMapItem&& > ItemMap;