代码之家  ›  专栏  ›  技术社区  ›  Yves

SFINAE的表现

  •  -1
  • Yves  · 技术社区  · 5 年前

    我早就知道了 if else 可以生成 Pipeline stall (bubble) if elif elif ... else

    在C++的模板中,我们有 SFINAE . 有了SFINAE,我们可以避免 否则

    template <int I> void div(char(*)[I % 2 == 0] = 0) {
        // this overload is selected when I is even
    }
    template <int I> void div(char(*)[I % 2 == 1] = 0) {
        // this overload is selected when I is odd
    }
    

    所以我们要避免

    if (I % 2 == 0)
    {
        // do things
    }
    else
    {
        // do other things
    }
    

    我的问题是,SFINAE是否比 否则 ? SFINAE能否避免管道气泡?

    1 回复  |  直到 5 年前
        1
  •  6
  •   Joseph Sible-Reinstate Monica    5 年前

    I 在编译时是可以知道的,在这种情况下,任何半正派的编译器都会为两者输出相同的常量大小写,或者不是,在这种情况下,SFINAE方法根本不会编译。