【问题标题】:MSVC's (seem not perfect) support for constexprMSVC(似乎并不完美)对 constexpr 的支持
【发布时间】:2018-09-28 08:52:09
【问题描述】:

我正在制作类型特征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");
}

这个demo可以用gcc/clang编译,但是不能用MSVC编译。 http://rextester.com/ATOC6638 http://rextester.com/IWU81465

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

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

【问题讨论】:

标签: c++ c++11 visual-c++ typetraits


【解决方案1】:

这几乎可以肯定是 MSVC 中的一个错误。尤其是以前的版本,constexpr 存在许多问题。例如Here'sjusta bunch of them。在 MSVC 中,对许多新功能的支持还不是那么好。但它正在变得更好。您将希望始终使用最新版本来尝试这种东西。 VisualStudio 2017 编译这段代码就好了……

【讨论】:

    【解决方案2】:

    您的代码使用 Visual Studio 2017(cl 版本 19.15.26726)编译。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2018-03-10
      • 2012-06-02
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多