wikipedia (here) gives a compile time unrolling of for loop....... i was wondering can we use a similar for loop with template statements inside... for example...
is the following loop valid
template<int max_subdomain>
void Device<max_sudomain>::createSubDomains()
{
for(int i=0; i< max_subdomain; ++i)
{
SubDomain<i> tmp(member);
...
// some operations on tmp
...
}
}
SubDomain is a class which takes in the a template parameter int and here has been constructed with an argument that is a member of the Device class.
Thanks for the answer guys... now that you know what i want... is there anyway i achieve what i want to??
i finally got what i wanted.............. instead of using the for loop directly... one can instead use the Boost::MPL for_each construct. I haven't yet implemented it but I am guessing that this provides a way to do what i wanted.....
I took the answer from another stack overflow question here... However the comment to the same question decries its use because it would be very slow (for large for loops of course)... however.. for loops which are not large i don't think there should be any bloating... i'll try the code and let you know the results....
the use is illustrated well in the example
Re-Edit:
My previous answer was correct. I have tried your code, it's giving compiler error. You cannot declare objects like that, as
i
cannot remain a compile time constant (as you are intending to doi++
).template
parameter must always be compile time constants. Here is the demo.Also note that, loop unrolling is done for normal loops also, as part of optimization by compilers. It's not limited to
template
s.Even though you already accepted @iammilind 's answer, let me propose another one, because his reasoning for why
i
is not compile-time-constantiswas not correct.Suppose you have
and you want to declare an instance of it ...
then
i
must be a commonly so-called compile-time-constant. What is that?Pedantry
The standard assigns the term
nontype template argument
to template arguments that are not types (D'Oh).14.3.2 Template non-type arguments [temp.arg.nontype]
Right the first point contains a reference for further research:
an integral constant-expression
. This leads us to5.19 Constant expressions [expr.const]
Then, the key is:
Pedantry application
If we look back at your loop:
then we can now observe that
i
is not allowed there. Why? Becausei
is NOT aconst variable
.The observing reader might now think you can circumvent this:
But is this really allowed? Negative,
I = i
is not an integral constant-expression, becausei
is not. It helps to realise that the rule for integral constant-expressions is applied recursively.E.g., the following is valid code:
But if make just one part of the chain non-const, then
ccK
is not considered integral constant anymore:Summary
So, in human readable form, template arguments that are not types, but (integer) values, must be compile-time-constant:
There's a stadard solution for this. Convert iteration into recursion.
Note: you can't use a runtime
if(i!=0) createSubDomains<i-1>();
.2017 Note: you can now use a compile-time
if constexpr(i!=0) createSubDomains<i-1>();