我需要运行一个带有 N 个布尔变量的函数,我想让它们 constexpr 以消除比较并从分支预测失败中保存代码。
我的意思是:
templateFunc<b1, b2, b3, b4 ...>(args...);
Run Code Online (Sandbox Code Playgroud)
由于 b1..bn 变量只是布尔变量并且可能只有 2 个状态,我可以这样写:
if (b1 && b2)
templateFunc<true, true>(args...);
else if (b1 && !b2)
templateFunc<true, false>(args...);
else if (!b1 && b2)
templateFunc<false, true>(args...);
else
templateFunc<false, false>(args...);
Run Code Online (Sandbox Code Playgroud)
问题很明显,我需要对 5 个变量进行 64 次调用。有什么解决方案吗?
c++ templates variadic-templates constexpr branch-prediction