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

MSVC对constexpr的支持(似乎并不完美)

  •  2
  • user8510613  · 技术社区  · 6 年前

    is_base_of . 这里有一个关于我的实现的小演示(没有考虑健壮性,是类…)。

    #include <type_traits>
    #include <cstdint>
    struct A
    {
    
    };
    struct B : A
    {
    
    };
    template
    <typename T, typename U>
    struct IsBaseOf {
        constexpr static bool Test(T* t)
        {
            return true;
        }
    
        constexpr static bool Test(...)
        {
            return false;
        }
    
        constexpr static bool value = IsBaseOf<T,U>::Test(static_cast<U*>(nullptr));
    };
    int main()
    {
        static_assert(IsBaseOf<A, B>::value, "Pass");
    }
    

    http://rextester.com/ATOC6638 http://rextester.com/IWU81465

    当我在笔记本电脑的visualstudio2015(更新补丁3)上输入时。它也不能编译,IDE提醒我在编译之前“表达式必须有常量值”。

    所以我想知道MSVC是如何支持constexpr的,还是我的代码错了?

    2 回复  |  直到 6 年前
        1
  •  3
  •   Michael Kenzel    6 年前

    这几乎可以肯定是MSVC中的一个bug。尤其是以前的版本有许多问题 constexpr . Here's just a bunch of them

        2
  •  0
  •   Swordfish    6 年前

    你可以尝试添加 /std:c++14 /std:c++latest 编译器开关。