Reputation: 4377
I'm studying A Tour of C++ and one of the demonstrations for Value Template Argument is to create a template class to arbitrarily allocate Buffer in the stack. I fail to see how it is different than simply create an array (i.e. int buf[100]) which will also be allocated from the stack?
Value arguments are useful in many contexts. For example, Buffer allows us to create arbitrarily sized buffers with no use of the free store (dynamic memory):
Buffer<char,1024> glob; // global buffer of characters (statically allocated)
void fct()
{
Buffer<int,10> buf; // local buffer of integers (on the stack)
// ...
}
Upvotes: 0
Views: 99
Reputation: 1
Most probably the Buffer
template class is similar to what's provided by std::array.
The big difference is, that with a class, all kinds of additional operations can be added, besides from raw c-style arrays, which cannot have any operators or other benefits you can use with classes.
Upvotes: 4