I’d like to use std::array from C++11 as a field of my own class. It takes two template parameters (first defines type of data, second defines size of an array).
But I know the second parameter only in constructor. I’m not familiar with C++11 standard, but I suppose that it’s impossible to set a template parameter during execution.
Are there any alternatives for std::array? std::vector is probably a little too much, cause I will never change the size of it.
std::vectoris the simplest thing to use; although as you say, it does waste a few bytes if you’ll never need to resize it.std::unique_ptr<T[]>, initialised using the result ofnew T[size], would be the most efficient thing; it should be the same size as a pointer, and will delete the allocated memory for you when it’s destroyed. It’s not copyable, though; you’ll need to provide a copy constructor for your class if you want it to be copyable. It’s also less convenient thanstd::arrayandstd::vector, since it doesn’t have the interface of a standard container. You could perhaps write a STL-style wrapper for it if you need that; but I’d just usestd::vectorin that case.