Программы на C++. Форум разработчиков
-
Anonymous
Как реализовать std :: any?
Сообщение
Anonymous »
Попробуйте реализовать крошечную: < /p>
Код: Выделить всё
#include
#include
#include
class TinyAny
{
public:
template
TinyAny(Tp&& val) : data(std::make_shared(sizeof(val))) {
new(data.get()) Tp(std::forward(val));
}
template
TinyAny &operator =(Tp&& val)
{
data = std::make_shared(sizeof(val));
new(data.get()) Tp(std::forward(val));
return *this;
}
template
Tp get()
{
return *reinterpret_cast(data.get());
}
private:
std::shared_ptr data;
};
int main() {
// var = "abc";
// std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/72877570/how-to-implement-stdany[/url]
1756992428
Anonymous
Попробуйте реализовать крошечную: < /p>
[code]#include
#include
#include
class TinyAny
{
public:
template
TinyAny(Tp&& val) : data(std::make_shared(sizeof(val))) {
new(data.get()) Tp(std::forward(val));
}
template
TinyAny &operator =(Tp&& val)
{
data = std::make_shared(sizeof(val));
new(data.get()) Tp(std::forward(val));
return *this;
}
template
Tp get()
{
return *reinterpret_cast(data.get());
}
private:
std::shared_ptr data;
};
int main() {
// var = "abc";
// std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/72877570/how-to-implement-stdany[/url]