为什么 C++ 中的全局函数外作用域没有 if-constexpr?

问题描述

if constexpr 不能在函数体外部使用有什么特殊原因吗(为什么它不在 C++ 中实现)?例如在下面的代码中使用 if constexpr 来决定子类的哪些特化:

template <int Size>
struct A {
    template <typename T> struct B;

    if constexpr(Size >= 32) {
        template <> struct B<uint32_t> {};
    }
    if constexpr(Size == 32) {
        using MaxT = uint32_t;
    }
    if constexpr(Size >= 64) {
        template <> struct B<uint64_t> {};
    }
    if constexpr(Size == 64) {
        using MaxT = uint64_t;
    }
};

上面的语法比使用 std::enable_ifstd::conditional_t 以及其他技巧要方便得多。

上面的语法允许根据编译时常量的值生成任何代码。在许多情况下,它可以很好地替代 #ifdef/#endif 宏。

作为@StoryTeller-UnslanderMonica 的 commented,其他语言中也存在类似的概念,例如 static if in D

有谁知道是否有关于此语法的任何建议和任何未来计划?如果他们被拒绝了,那为什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)