我有以下辅助函数:
template<typename T, std::size_t N>
constexpr std::size_t Length(const T(&)[N]) {
return N;
}
返回静态数组的长度。过去这总是有效的,但当我执行以下操作时:
struct Foo
{
unsigned int temp1[3];
void Bar()
{
constexpr std::size_t t = Length(temp1); // Error here
}
};
我在使用MSVS 2017时遇到了错误:
error C2131: expression did not evaluate to a constant
note: failure was caused by a read of a variable outside its lifetime
note: see usage of 'this'
我希望有人能够解释一下我做错了什么。
Length
标记为noexcept
- 没有任何合理的情况会导致它抛出异常。 - Jesper Juhl-pedantic-errors
也会失败。 - AnT stands with Russia