473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dynamically choosing a template parameter at runtime

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[i]};
};

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[S];
virtual Generic*& operator[](size_t i){return dat[i];};
};

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[i];};
};

//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?
Jul 24 '08 #1
4 7914
On 24 jul, 03:15, alan <almkg...@gmail.comwrote:
AFAIK a template parameter must be completely determinable by the
compiler at compile time, is this correct?
Yes, it's 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[i]};

};

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[S];
* * * * virtual Generic*& operator[](size_t i){return dat[i];};

};

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[i];};

};

//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?

Templates are compile-time mechanisms. On the other hand, virtual
stuff are run-time mechanisms. (That's why a template function cannot
be virtual.) I suggest you to write this classes using either
templates or virtuals, not mixing them. If you want to stick with the
dynamic world, std::vector is a choice, just as you did.

Keep in mind that with templates you can almost always achieve the
same functionality of dynamic polimorphism with techniques that allow
you to design static polimorphism. You loose the ability to create
heterogeneous collections (or similars), but there's usually some
workaround. However, there other benefits like, for example,
performance gains.

--
Leandro T. C. Melo
Jul 24 '08 #2
On Jul 24, 5:15*pm, alan <almkg...@gmail.comwrote:
AFAIK a template parameter must be completely determinable by the
compiler at compile time, is this correct?
Yes.
>
I'd like to ask if there's a better way?
That's a pretty good way and most likely just fine.

There is a way to use a generic factory pattern to do the same thing
but one can debate if it's worth the trouble. You will still need to
register all the sizes you care for.

Jul 25 '08 #3
I'd like to ask if there's a better way?
>
That's a pretty good way and most likely just fine.

There is a way to use a generic factory pattern to do the same thing
but one can debate if it's worth the trouble. *You will still need to
register all the sizes you care for.
Err, I thought what I already did was a factory pattern?

Or is "generic factory pattern" something even more than what I just
did??

Jul 25 '08 #4
On Jul 24, 7:41*pm, Leandro Melo <ltcm...@gmail.comwrote:
>
I'd like to ask if there's a better way?

Templates are compile-time mechanisms. On the other hand, virtual
stuff are run-time mechanisms. (That's why a template function cannot
be virtual.) I suggest you to write this classes using either
templates or virtuals, not mixing them. If you want to stick with the
dynamic world, std::vector is a choice, just as you did.
None of the virtual functions are templated. The template I'm using
is a size template, since I would like to make a class with a variable-
size array, which I'd prefer to be part of the class object.

And std::vector isn't so good a choice: it allocates using the default
allocator. I know it can use a different allocator, the problem is
that my memory manager is intrusive, so std::vector will have to
allocate a class that ultimately derives from Generic, my intrusive
class. But std::vector allocates a plain memory area, which my
intrusive memory manager cannot handle.

The main reason I'm mixing templates and virtuals is that I need my
class to be variable size, but still present the same interface, i.e.
the Closure interface.
Jul 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
4
by: Robert | last post by:
Are there any other ways to dynamically apply CSS styling to a page (without using inline CSS)? I'm sure I could dynamically generate a new and uniquely named CSS file "on the fly" when users...
2
by: Suma | last post by:
I have a problem with editable datagrid and was hoping if anyone could help me. Please help me if possible. I have an editable datagrid, whose column count I don’t know until runtime. I am...
1
by: Marcus | last post by:
I have a problem maybe one of you could help me with. I've created a data entry screen with lots of dynamically-created client-side controls. I create HTML texboxes client-side by assigning a...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
4
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.