我尝试在以下情况下使用复制分配。
有两个模板类,list map
and xpair
.
template <typename Key, typename Value, class Less=xless<Key>>
class listmap {
public:
using key_type = Key;
using mapped_type = Value;
using value_type = xpair<const key_type, mapped_type>; //value_type
...
}
template <typename First, typename Second>
struct xpair {
First first{};
Second second{};
xpair(){}
xpair (const First& first, const Second& second):
first(first), second(second) {}
};
在 main.cpp 中,我尝试编写,
using commend = string;
using str_str_map = listmap<string,string>;
using str_str_pair = str_str_map::value_type; //value_type, to be replaced
using commend_pair = xpair<commend, str_str_pair>;
int main(...) {
commend_pair cmd_pair;
str_str_pair newPair ("Key", "value");
cmd_pair.second = newPair;
...
}
它给了我一个错误说
object of type 'xpair<const std::__1::basic_string<char>,
std::__1::basic_string<char> >' cannot be assigned because its copy
assignment operator is implicitly deleted
如果我更换
using str_str_pair = str_str_map::value_type;
to
using str_str_pair = xpair<string, string>;
一切正常。这是为什么?不应该value_type = xpair<string, string>
?