Explicitly instantiate?
The above example I posted clearly shows how to use it and how it's more fool-proof than passing in size.
What do you mean?
Well, obviously, this kind of thing won't work:
But as long as you keep the type information through proper references, it will work.Code:#include <iostream>
template<size_t N>
void foo(int (&bar)[N])
{
std::cout << N << std::endl;
}
int main()
{
int bla[50];
int* r = bla;
foo(*r);
}
Though, I am not entirely sure of what scenario you had in mind.
>Well... I use C Style arrays only in analogy to pointers...
>>What do you mean?
Foo* foo = new Foo[n];
But what was your point? How do you relate it to the ongoing discussion?
But you can use std::vector. So why don't you?
vector as well as most STL containers (except array in fact) use the heap.
C++ does not manage memory in the sense that what you new, you must delete (though there are tools that help you do that, such as smart pointers).
If you use containers, they manage that memory for you. There is almost never any need to allocate a container on the heap.
std::vector and almost all other containers need to dynamically allocate memory. Hence, the data they store is often allocated on the heap. The vector itself is up to you to place (ie, on the stack or the heap).
When there is?
Suppose...
As of my current project: I have a class that privately stores a std::vector of std::map `s ...of which 3 Global Objects are declared to be used from all the other modules somewhat like environment variables.
Whenever I am adding a map to one of those, I'm allocating ..and then passing its value to the relevant function to 'push' it.
I'm not sure.. but would it work if I just create a map and pass it ?