Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Getting rid of ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 9355 of 10087
Post > Topic >>

Getting rid of repeating types in template instantiations

by rsergeant <rsergeant@[EMAIL PROTECTED] > Mar 6, 2008 at 05:42 PM

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.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 5 Posts in Topic:
Getting rid of repeating types in template instantiations
rsergeant <rsergeant@[  2008-03-06 17:42:26 
Re: Getting rid of repeating types in template instantiations
Jeff Schwab <jeff@[EMA  2008-03-07 03:33:12 
Re: Getting rid of repeating types in template instantiations
happyasaclam111@[EMAIL PR  2008-03-07 03:42:26 
Re: Getting rid of repeating types in template instantiations
Triple-DES <DenPlettfr  2008-03-07 03:42:27 
Re: Getting rid of repeating types in template instantiations
Carl Barron <cbarron41  2008-03-07 03:42:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 17:18:10 CDT 2008.