Эта проблема возникла у меня при попытке реализовать контейнер типа std::any
.
Является ли использование const
в placement new
излишним?
Если нет, что это значит?
Должен ли я использовать std::decay
на placement new
?
#include <iostream>
int
main() {
auto * address = std::malloc(sizeof(std::string));
// What does `const` means here?
// Is it superfluous?
// Is `std::decay` needed here too?
new (address) std::string const("hello, world");
// Is this undefined behaviour?
// In the context of my code: T -> std::decay<T>
// Here I'm just using a `std::string` as an example
auto & str = *static_cast<std::string *>(address);
str.append("hi");
std::cout << str << '\n';
return 0;
}