#include <cstddef>
#include <utility>
#include <type_traits>
// В реальном коде не забудьте про выравнивание
// И про защиту типа
template<size_t N = 32> struct any
{
template<class t> any(t&& a)
{
new(buf) t(std::forward<t>(a));
}
template<class t, class... args>
t& make(args&&... params)
{
auto* p = new(buf) t(std::forward<args>(params)...);
return *p;
}
template<class t> operator t() noexcept
{
using x = std::remove_reference_t<t>;
x* p = reinterpret_cast<x*>(this->buf);
return *p;
}
char buf[N];
};
#include <iostream>
#include <vector>
#include <string>
struct IGPIGP
{
template<class ostream> friend
ostream& operator<<(ostream& out, const IGPIGP& obj)
{
out << "IGPIGP: " << obj.v;
return out;
}
int v;
};
int main()
{
std::vector< any<> > container
{
std::string{"hello"},
IGPIGP{1},
std::string{"world"},
IGPIGP{2}
};
std::cout
<< static_cast<std::string>(container[0]) << '\n'
<< static_cast<IGPIGP>(container[1]) << '\n'
<< static_cast<std::string>(container[2]) << '\n'
<< static_cast<IGPIGP>(container[3]) << '\n';
}