A
alan
AFAIK a template parameter must be completely determinable by the
compiler at compile time, is this correct?
Currently my problem is, I have a class which contains a std::vector.
The size of this vector can be determined at the time an object of the
class is constructed, and is assured not to change over the lifetime
of the object.
Now this class is part of an intrusive memory pool management scheme,
and while it's okay for the std::vector to use standard allocations,
I'd prefer that the underlying array be part of the object, so that
the memory manager will handle the entire memory of that class,
instead of just (effectively) the pointer, size, and capacity.
//Generic is the intrusive memory pool
//management base class
class Closure : public Generic {
public:
....
Closure(size_t S) : dat(S) {}
std::vector<Generic*> dat;
Generic*& operator[](size_t i){return dat};
};
What I'd rather have:
class Closure : public Generic {
public:
....
virtual Generic*& operator[](size_t i)=0;
};
template<size_t S>
class ClosureA : public Closure{
public:
Generic* dat;
virtual Generic*& operator[](size_t i){return dat;};
};
However the size parameter must be determined at runtime, not compile
time:
// current use!
void do_something(Heap& h, size_t N){
Closure *s = new(h) Closure(N);
....
}
The best solution I could come up with is:
// fallback on vectors
class ClosureV : public Closure{
public:
ClosureV(size_t S) : dat(S){}
std::vector<Generic*> dat;
virtual Generic*& operator[](size_t i){return dat;};
};
//factory function
Closure* NewClosure(Heap& h,size_t S){
switch(S){
case 0: return new(h) ClosureA<0>();
case 1: return new(h) ClosureA<1>();
case 2: return new(h) ClosureA<2>();
case 3: return new(h) ClosureA<3>();
case 4: return new(h) ClosureA<4>();
case 5: return new(h) ClosureA<5>();
case 6: return new(h) ClosureA<6>();
case 7: return new(h) ClosureA<7>();
case 8: return new(h) ClosureA<8>();
// give up; not likely to occur, but still might...
default: return new(h) ClosureV(S);
}
}
I'd like to ask if there's a better way?
compiler at compile time, is this correct?
Currently my problem is, I have a class which contains a std::vector.
The size of this vector can be determined at the time an object of the
class is constructed, and is assured not to change over the lifetime
of the object.
Now this class is part of an intrusive memory pool management scheme,
and while it's okay for the std::vector to use standard allocations,
I'd prefer that the underlying array be part of the object, so that
the memory manager will handle the entire memory of that class,
instead of just (effectively) the pointer, size, and capacity.
//Generic is the intrusive memory pool
//management base class
class Closure : public Generic {
public:
....
Closure(size_t S) : dat(S) {}
std::vector<Generic*> dat;
Generic*& operator[](size_t i){return dat};
};
What I'd rather have:
class Closure : public Generic {
public:
....
virtual Generic*& operator[](size_t i)=0;
};
template<size_t S>
class ClosureA : public Closure{
public:
Generic* dat
virtual Generic*& operator[](size_t i){return dat;};
};
However the size parameter must be determined at runtime, not compile
time:
// current use!
void do_something(Heap& h, size_t N){
Closure *s = new(h) Closure(N);
....
}
The best solution I could come up with is:
// fallback on vectors
class ClosureV : public Closure{
public:
ClosureV(size_t S) : dat(S){}
std::vector<Generic*> dat;
virtual Generic*& operator[](size_t i){return dat;};
};
//factory function
Closure* NewClosure(Heap& h,size_t S){
switch(S){
case 0: return new(h) ClosureA<0>();
case 1: return new(h) ClosureA<1>();
case 2: return new(h) ClosureA<2>();
case 3: return new(h) ClosureA<3>();
case 4: return new(h) ClosureA<4>();
case 5: return new(h) ClosureA<5>();
case 6: return new(h) ClosureA<6>();
case 7: return new(h) ClosureA<7>();
case 8: return new(h) ClosureA<8>();
// give up; not likely to occur, but still might...
default: return new(h) ClosureV(S);
}
}
I'd like to ask if there's a better way?