'C++ enum class as a variable template parameter
I'm trying to optimize out some function at compile time by using an enum class as a template parameter.
Say for example
enum class Color { RED, BLACK };
Now, I would like to define a method
void myMethod<Color c> () {
if( c == Color::RED ) { ... }
if( c == Color::BLACK ) { ... }
}
And I would like the compiler to make 2 copies of myMethod and eliminate the dead code during the optimisation (it's for CUDA kernels so speed and register usage is important to me)
However, seems like when I call the method using
void doSomething( const Color c ) {
myMethod<c>();
}
MSVC complains with "expression must have a constant value".
I was expecting the compiler to be clever enough to compile a version of myMethod with each possible version of the enum. Is that not the case ? Can I force it to, without an ugly switch in doSomething ?
Thanks for your help !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
