C ++ - Copy Assignment Operator Implicitly Deleted

I am trying to use copy assignment in the following case.

There are two classes of patterns: list mapand 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) {}
};

In main.cpp I tried to write

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;

   ...
}

It gives me an error saying

  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

If I replaced

using str_str_pair = str_str_map::value_type;

to

using str_str_pair = xpair<string, string>;

Everything works perfectly. Why is this? Shouldn't value_type = xpair<string, string>?

+4
source share
2 answers

I don’t see where it is being advertised newPair, but the error message seems enough.

: pair const, . const string, , , , pair<const string, T>.

std::pair<const int, int> p(0, 0);
p.first = 1; // no, can't assign to p.first
p = std::pair<const int, int>(1, 2); // no, requires assigning to p.first

const : map . , . :

std::map<string, int> m = { ... };
auto it = m.find(k);
it->first = "a different value";

a std::map, , - , node. pair node. key m, key.

+5

using value_type = xpair<const key_type, mapped_type>;

const key_type, , key_type const.

using str_str_pair = xpair<string, string>;

string const

+4

Source: https://habr.com/ru/post/1628014/


All Articles