My project uses custom allocator that constructs objects the following way:
template <typename T, typename... Args>
T* create(Args... args)
{
T* object = // request "bare" memory
// call object constructor
return new(reinterpret_cast<void*>(object)) T(args...);
}
There is a class, containing const reference as a field:
struct A {
A(const string& s) : str_(s) {
cout << &s << '\n';
}
const string& str_;
};
But when I'm trying to create an object, I'm getting wrong results:
string* str = new string("some string");
cout << str << '\n';
A* a = create<A>(*str);
The output:
0x7fffc8004db4
0x7fffd5436050
I thought the const reference field (str_
) should contain the same address, that has been given to construction machinery, but obviously it isn't true.
Why it is happen, and how to avoid it?
Ofc, I can not help using custom allocators, it is mandatory, I wouldn't ask.
Aucun commentaire:
Enregistrer un commentaire