I have this code:
#include <iostream>
#include <string>
#include <unordered_map>
class Y {
public:
Y()=default;
Y(const Y&)=delete;
};
class Z{
public:
Z()=default;
Z(const Z&)=delete;
Y y_;
};
class A {
public:
A()=default;
A(const A&)=delete;
std::unordered_map<int, Z> map;
};
class B : public A{
public:
B()=default;
};
A a;
A * b = new B;
void GetB(B & b) {
b = *(reinterpret_cast<B *>(&b)); //<== this line causing the error
}
int main()
{
B b;
GetB(b);
}
When compiling I get the following error:
error: use of deleted function 'constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = Z]'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
If I change the marked line like so:
void GetB(B * b) {
b = (reinterpret_cast<B *>(&b));
}
I don't get that error, and the code compile fine. It seems like this error is caused by the fact that I've deleted the copy constructor of class B. But I don't understand why the marked line in the code executes B's copy constructor in the first place?
b
is reference, and I expect it to be assigned by reference, and not by value. Does it have something to do with using unordered_map
and std::pair
? This isn't happening when using vector
instead of unordered_map
Aucun commentaire:
Enregistrer un commentaire