The following code does not compile. Is it even possible to pass an instance of Configuration<t, u>
as a template parameter? (to profit from optimization of constant expressions)
API:
template <int t, bool u>
struct Configuration {
static constexpr int commonValue = t;
static constexpr bool debug = u;
};
template <
template<int t, bool u> Configuration<t, u> &conf,
int x
>
class A {
public:
int doSomething() {
if (conf.debug) { // Optimize away if false
// ...
}
return x * conf.commonValue; // Optimize to e.g. 6 with `conf.commonValue` =
// 3, `x` = 2
}
};
The user of the API should be able to do:
int main() {
static constexpr Configuration<3, false> conf;
A<conf, 2> a;
A<conf, 5> b;
A<conf, 8> c;
std::cout << a.doSomething() << std::endl; // 6 (evaluated at compile time!)
std::cout << b.doSomething() << std::endl; // 15
std::cout << c.doSomething() << std::endl; // 24
}