代码之家  ›  专栏  ›  技术社区  ›  Chen Li

从语言设计的层面来看,当条件在编译时无法推导时,“if constexpr”为什么不衰减为“trival if”

  •  3
  • Chen Li  · 技术社区  · 7 年前

    constexpr function compile-time ,将延迟计算 run-time non-constexpr function constexpr

    if statement if constexpr compile-time if statement 容易(没有 true_type false_type

    constexpr int factorial(int n)
    {
        if constexpr(n == 0) return 1;
        else return n * factorial(n-1);
    }
    

    所以,上面的代码 cannot pass compilation n the function can be calculated at compile-time when input is known at compile-time

    1 回复  |  直到 7 年前
        1
  •  1
  •   StoryTeller - Unslander Monica    7 年前

    如果不满足程序中的约束条件,则是编写并依赖它的人

    b

    推荐文章