How can I instantiate a template with a type determined at runtime?
Hello,
I need to instantiate a std::vector, but its type depends on something that I would not know until runtime:
#include <vector>
if (some_condition)
std::vector<float> myVector;
else
std::vector<int32_t> myVector;
I know this does not work because,in order to instantiate a template class, the data type must be known at compile time.
I've seen some people suggest std::variant but I have not understood it completely.
Also, how do different libraries (like ONNX) provide different data types? Do they use templates?
PS : I am not developing a library.
EDIT:
I need to create an array or a vector to act as a placeholder (allocator) for input tensors. But this vector requires a different amount of space based on the size and type of the input. The size is easily manageable per instance of the parent class if we only had one type, but we don't.
The thing is, there are some arguments that need to be set for this specific project that determine what exactly the size of the array would be, and they are only known when you parse an ONNX model at runtime.