问题描述
我有一个性能至关重要的内联函数inline T func(T a, T b, int p)
.如果已知p为零,则可以对其进行相当多的优化.但是,我不能使用'if'来惩罚其他所有情况.我想要的是仅在编译时知道p为零的情况下优化函数.有没有一种干净的方法可以做到这一点,也许可以使用模板魔术?
I have a performance critical inline function, inline T func(T a, T b, int p)
. It can be optimized quite a bit, if p is known to be zero. However, I can't use an 'if' and penalize all the the other cases. What I want is to optimize the function only of I know at compile-time that p is zero. Is there a clean way to do that, maybe using template magic?
编辑
我不能使用不同名称的函数/不兼容的重载(我不知道如何正确地表达这一点),因为代码的级别很低.在另一个/将来的处理器上很可能有不同的优化机会.因此,通过将所有低级内容限制在一个函数签名中,只需重新编译我的代码即可使用任何类型的优化.
I can't use differently named function/incompatible overloads (I don't know ho to express that correctly) since the code is very low level. It is very likely, that different optimization opportunities are available on another/future processors. Thus by confining all that low level stuff in one function signature, my code only needs to be recompiled to use any type of optimization.
推荐答案
可以使用模板.看看增强enable_if ,它有很好的解释和用法示例
it's possible using templates. have a look at Boost enable_if, it has good explanation and usage examples
这篇关于如何根据编译时间参数使用内联函数的不同重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!