K
Kristian Bisgaard Lassen
Hi,
How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.
BTW I am new to the C++ language and also the templates it provides.
Here is an example of what I want to do
template<bool condition, class Then, class Else>
struct IF {
typedef Then RET;
};
template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else RET;
};
struct A {
static void exec () {
cout << "yes" << endl;
}
};
struct B {
static void exec () {
cout << "no" << endl;
}
};
template<const int mask[]>
void foo () {
IF<mask[0] == 1,
A,
B>::RET::exec ();
}
struct Run {
static void run () {
static const int filter[] = {1,1,1};
foo<filter>();
}
};
int main () {
Run::run();
return 0;
}
I want the compiled code just to contain the instructions for
cout << "yes" << endl;
But as I said g++ complains and says
error: non-constant `((*&filter) == 1)' cannot be used as template
argument
The error is from the statement
IF<mask[0] == 1,
A,
B>::RET::exec ();
Hope their is someone who can tell me what I am doing wrong or if is
even possible make compiletime optimizations using arrays.
Best regards,
Kristian Bisgaard Lassen
How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.
BTW I am new to the C++ language and also the templates it provides.
Here is an example of what I want to do
template<bool condition, class Then, class Else>
struct IF {
typedef Then RET;
};
template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else RET;
};
struct A {
static void exec () {
cout << "yes" << endl;
}
};
struct B {
static void exec () {
cout << "no" << endl;
}
};
template<const int mask[]>
void foo () {
IF<mask[0] == 1,
A,
B>::RET::exec ();
}
struct Run {
static void run () {
static const int filter[] = {1,1,1};
foo<filter>();
}
};
int main () {
Run::run();
return 0;
}
I want the compiled code just to contain the instructions for
cout << "yes" << endl;
But as I said g++ complains and says
error: non-constant `((*&filter) == 1)' cannot be used as template
argument
The error is from the statement
IF<mask[0] == 1,
A,
B>::RET::exec ();
Hope their is someone who can tell me what I am doing wrong or if is
even possible make compiletime optimizations using arrays.
Best regards,
Kristian Bisgaard Lassen