我正在制作traits类型的is_base_of的幼稚轮子。这是关于我的实现的最小演示(未考虑健壮性,is_class ...)。

#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");
}

该演示可以由gcc / clang编译,但不能由MSVC编译。
http://rextester.com/ATOC6638
http://rextester.com/IWU81465

当我在笔记本电脑的Visual Studio 2015(带有更新补丁3)上键入它时。它也不能编译,IDE提醒我在编译之前“表达式必须具有恒定值”。

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

最佳答案

这几乎可以肯定是MSVC中的错误。特别是以前的版本,constexpr存在许多问题。 Here's just a bunch of them。在MSVC中,对许多新功能的支持还不是很好。但是现在情况正在好转。您将要始终使用最新版本来尝试这种方法。 VisualStudio 2017可以很好地编译此代码…

09-10 04:10
查看更多