Boost logo

Boost Users :

From: Beth Jacobson (bethj_at_[hidden])
Date: 2006-05-02 10:58:27


Dale McCoy wrote:
> On 5/2/06, chun ping wang wrote:
>> Its more of a c++ question but i wonder how would I pass constant variables
>> into template parameters such as nth_index_iterator.
>> Example: void func(const int& var)
>> {
>> nth_index_iterator<var>::type;
>> }
>>
>> Is this possible whats the work around this limitation?
>
> Template parameters must be known at compile-time. If var is known at
> compile time, changing it to a #define should work. If var is not
> known at compile time, then you can't use it as a template parameter;
> you'll have to find something else that'll do the job (eg
> std::vector<bool> rather than std::bitmask<N>).
>
> Strange tricks involving dynamic compiling and linking might work, but
> that would be several different types of difficult.
>
> Dale

I've been working on something similar, and the best I've been able to
come up with is this:

void func(const int& var)
{
     switch (var)
     {
     case 1:
         template_func<1>();
         break;
     case 2:
         template_func<2>();
         break;
     // etc
     }
}

If you have a limited range of var values, this should work fine. It's
pretty tedious though, so if anyone can suggest an alternative not
involving those "several different types of difficult", it would be much
appreciated.

Beth


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net