I have a class that uses boost :: variant to store a double or string, for example:
class value
{
boost::variant<double, std::string> val;
};
It should be an invariable type of value for the toy translator I play with. At first, it would be nice to pass this by a const reference and return by value, and it was always allocated on the stack, since I wanted it to be considered as a primitive. However, then I saw that its size is 40 bytes (due to sizeof std :: string, basically), and I was a bit worried. I know that I should not allocate large chunks of memory on the stack, but how big is the volume?
In addition, every time I return, copying 40 bytes, especially since the value is unchanged and does not even need to be copied, it seems to be part of the waste.
The option of regular heap allocation does not seem too attractive, since I could have thousands of these allocations / releases per second.
The last option I came across is to have boost :: pool to distribute these objects when necessary, and use boost :: shared_ptr to control your life. However, since the interpreter is responsible for memory allocation (the type of memory allocation will be the policy passed to the interpreter as the template argument), this will mean that the value class must know about the interpreter, which complicates the situation a bit.
So these are the questions:
- What should I do in this case and why?
- How big is "too big" to fit on the stack? I am sure it depends on how often it stands out and how often it should also be copied.
Thank.