passing variable into template parameters.

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?

You can pass constant to template parameter. But you cannot pass constant reference. Look: const int var=1; nth_index_iterator<var>::type; //ok const int& var2=var; nth_index_iterator<var2>::type; //error _____ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of chun ping wang Sent: Tuesday, May 02, 2006 1:54 PM To: boost-users@lists.boost.org Subject: [Boost-users] passing variable into template parameters. 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?

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

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

I've been working on something similar, and the best I've been able to come up with is this: <snip> If you have a limited range of var values, this should work fine. It's pretty tedious though,
That looks pretty well suited to (semi-)automated generation, either with a separate script, or with something like: #define _(x) case x: template_func<x>(); break; void func(const int& var){ switch (var){ _(1) _(2) // etc } } #undef _ This doesn't solve the limited range of values problem, though. Dale
participants (4)
-
Beth Jacobson
-
chun ping wang
-
Dale McCoy
-
vlad