#include <iostream>
#include <cstdlib>
template<class To, class From>
To any_cast(From v)
{
    return static_cast<To>(static_cast<void*>(v));
}
class Something
{
public:
    template<class T>
    Something(T mydata);
    template<class T>
    void getData(T& t);
    ~Something();
private:
    unsigned char* data;
};
int main()
{
    {
        Something s(20.1);
        double ret = 0;
        s.getData(ret);
        std::cout << ret << std::endl;
    }
    return 0;
}
template<class T>
Something::Something(T mydata)
{
    data = new unsigned char[sizeof(mydata)];
    unsigned char* temdata = any_cast<unsigned char*>(&mydata);
    for (unsigned int i = 0; i < sizeof(mydata); i++)
    {
        data[i] = temdata[i];
    }
}
Something::~Something()
{
    delete[] data;
}
template<class T>
void Something::getData(T& t)
{
    T* tt = any_cast<T*>(data);
    t = *tt;
}
In the above example I take an object and turn it into an unsigned char array, or a byte array. Afterwords when the class deconstructor is called I delete this variable. However I seem to keep hitting breakpoints in my program when I delete the object "data".
I assume this is due to it thinking its a normal char* and looking for the terminating /0 to denote the end of the string.
I know since I'm allocating data i need to DE-allocate it, so what is the safest way to do so. Or is it even necessary?
Thought about using a std::vector and that would solve the problem. But I don't want the performance hit on data retrieval if the object is scattered across the heap.
I also tried both delete operators. I get a:
HEAP[test.exe]: Invalid address specified to RtlValidateHeap( 01380000, 04574468 )
Error with either one. (Sorry the delete was a typo)
The code is simplified from the original code, so I did not include the copy constructors and ext.
After seeing usage of std::any I tried it out in a test enviroment: Here and that seems to work alot better then my idea. But now I need to figure out how to make visual studios use a higher level compiler with access to c++17 features =/
 
    