MSVC(似乎并不完美)对 constexpr 的支持
MSVC's (seem not perfect) support for constexpr
我正在制作类型特征的天真轮子 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提醒我编译前"expression must have constant value"。
所以我想知道 MSVC 对 constexpr 的支持如何,还是我的代码有误?
这几乎可以肯定是 MSVC 中的错误。特别是以前的版本在 constexpr
方面有很多问题。 Here's just a bunch of them 例如。 MSVC 对许多新功能的支持还不是很好。但它正在变得更好。您将希望始终使用最新版本来尝试此类内容。 VisualStudio 2017 可以很好地编译这段代码…
您的代码使用 Visual Studio 2017(cl 版本 19.15.26726)编译。
您可以尝试添加 /std:c++14
或 /std:c++latest
编译器开关。
我正在制作类型特征的天真轮子 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提醒我编译前"expression must have constant value"。
所以我想知道 MSVC 对 constexpr 的支持如何,还是我的代码有误?
这几乎可以肯定是 MSVC 中的错误。特别是以前的版本在 constexpr
方面有很多问题。 Here's just a bunch of them 例如。 MSVC 对许多新功能的支持还不是很好。但它正在变得更好。您将希望始终使用最新版本来尝试此类内容。 VisualStudio 2017 可以很好地编译这段代码…
您的代码使用 Visual Studio 2017(cl 版本 19.15.26726)编译。
您可以尝试添加 /std:c++14
或 /std:c++latest
编译器开关。