possibly silly question: I am trying to do the following
template<unsigned int N>
class Foo{...}; // Define class Foo accordingly
int main(){
for (unsigned int i = 0; i < 10; i++){
Foo<i> f(3);
f.show();
}
return 0;
}
As you can imagine, it won't compile since the variable i
is not const
. I know the reason for this is that the values assigned inside the non-type template argument Foo<i>
must be known at compile time and since this is not the case here, it doesn't really know what to do. Now, I was wondering if there is a way to overcome this problem. First idea is, of course, to declare unsigned int N
as member-variable of class Foo.
So question: Is it possible to achieve the desired behaviour above using template arguments, or do I have to declare unsigned int N
as member variable of my class?
P.S.: I've tried to find a related to question, but the questions I've found were related to how to use non-type template parameters, where they didn't quite answer the question. Since they didn't mention that this is possible I assume this can't be done...
Edit.
Is it possible to do the following?
template<unsigned int N>
class Foo{...}; // Define class Foo accordingly
int main(){
std::vector<Foo> v; // I know it's better with shared_ptr, etc.. but to get the idea..
for (unsigned int i = 0; i < 10; i++){
Foo<i> f(3);
f.show();
v.push_back( f );
}
return 0;
}