Re: template question: preallocation for the underlying deque in user-defined queue
On 4 Okt., 00:37, jg <jgu...@gmail.com> wrote:
On Oct 3, 12:58 pm, Heck <hec...@inordertostymieharvestersverizon.net>
You can try the following:
template <class T> class QUEUE2 {
...
std::deque<T> *c;
....
QUEUE2( int prealloc) {
c = new std::deque<T> (prealloc);
}
Although possible, this is not recommended here. Now the OP
has to define copy c'tor, assignment op, and the destructor
as well with no real advantage compared to the simple way.
Or
template <class T, int n> class QUEUE2 {
...
std::deque<T> c(n);
...}
where I think 'n' must be compile-time constant when
instantiating QUEUE2.
This is currently no valid C++, albeit their exists a proposal which
would allow exactly this, see:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2354.htm
What you are thinking of is only possible for static constants
of integral or enumeration type where the initialization expression
is an ICE. This constraint will be probably lifted somewhat in
C++0x, but also only for static members of literal types which
are initialized with a constant expression, see the most recent
draft N2369.
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]