On Feb 5, 6:05 am, potapo...@gmail.com wrote:
Option 1, "switch" statement is much faster than "if" one:
Base& Factory::createInstance(int i) {
switch(i){
case 1: return C<1>();
case 2: return C<2>();
...
}
}
Option 2, linear recursive helper function:
const int max_n = 100;
template<int n>
struct helper
{
static Base& func(int i)
{
return i == n? C<n>() : helper<n+1>::func(i);
}
};
template<>
struct helper<max_n>
{
static Base& func(int i)
{
return C<max_n>();
}
};
Base& Factory::createInstance(int i) {
return helper<0>(i);
}
<snip>
Base& func0()
{
return C<0>();
}
Base& func1()
{
return C<1>();
}
Base& func2()
{
return C<2>();}
...
Base& Factory::createInstance(int i) {
const Base& (*ptrs)()[]={func0,func1,func2,... /* can be filled by
Boost.Preprocessor too */};
return ptrs[i]();
}
Unfortunately all of those examples fail as they are returning
references to locals that go out of scope.
An appropriate fix for all of them, including the helper version which
I quite like, is to make a local static object like this:
Base & func0()
{
static const C<0> item;
return item;
}
BTW this pattern is very useful in cases where you benchmarked your
code and absolutely need the optimization of template specializations
to factor out variable coefficients into constants.
safe in most implementations of C++03. It will be thread-safe in C+
+0x.
[ comp.lang.c++.moderated. First time posters: Do this! ]