【发布时间】:2019-07-04 16:08:10
【问题描述】:
我有以下代码:
#include <iostream>
template<int I>
class A
{
public:
inline constexpr static int size() { return I; }
};
template<typename T>
inline constexpr auto size(const T& arg) noexcept -> decltype(arg.size())
{
return arg.size();
}
template<typename T>
inline constexpr void twoLevel(const T& arg) noexcept
{
static_assert(size(arg) > 0);
}
int main()
{
A<5> a;
static_assert(size(a)>0); //this works
twoLevel(a); // this does not
return 0;
}
无法在 msvc 上编译并出现错误 expression did not evaluate to a constant,但适用于 gcc。 gcc 是否接受了未定义的行为?或者它是 msvc 的编译器错误?
这是一个演示:godbolt code
【问题讨论】:
标签: c++ templates c++17 constexpr compile-time-constant