【发布时间】:2021-09-18 12:44:33
【问题描述】:
以下代码在使用最新的 GCC 和 Clang 稳定版本时编译良好。
但是,MSVC 给出了一个错误,即矩阵的初始化不会计算为常数。这是一个 MSVC 错误还是我错过了什么?
<source>(72): error C2131: expression did not evaluate to a constant
<source>(71): note: failure was caused by out of range index 4; allowed range is 0 <= index < 4
#include <cassert>
#include <cstring>
#include <iostream>
#include <utility>
#include <stdexcept>
struct index_t final
{
constexpr index_t() = default;
constexpr index_t(const std::size_t r, const std::size_t c) :
row{ r },
col{ c }
{
}
std::size_t row{};
std::size_t col{};
};
template<typename type_t, std::size_t rows_v, std::size_t columns_v>
class matrix_t final
{
public:
explicit constexpr matrix_t(const type_t(&values)[rows_v][columns_v])
{
for (std::size_t row = 0; row < rows_v; ++row)
{
for (std::size_t col = 0; col < columns_v; ++col)
{
m_values[row][col] = values[row][col];
}
}
}
constexpr index_t get_index_for_value(const type_t& value) const
{
for (std::size_t row = 0; row < rows_v; ++row)
{
for (std::size_t col = 0; col < columns_v; ++col)
{
if (m_values[row][col] == value)
{
return index_t{ row,col };
}
}
}
throw std::invalid_argument("value not found in array");
}
constexpr auto operator[](const std::size_t row) const
{
return &m_values[row][0];
}
private:
type_t m_values[rows_v][columns_v]{};
};
// Intialization of matrix : error C2131: expression did not evaluate to a constant
// message : failure was caused by out of range index 4; allowed range is 0 <= index < 4
constexpr matrix_t matrix
{ {
{ 18, 39, 91, 91 },
{ 67, 3, 9, 95 },
{ 69, 3, 68, 93 },
{ 69, 21, 92, 92 }
} };
int main()
{
static_assert(matrix[1][1] == 3);
static_assert(matrix.get_index_for_value(68).row == 2);
static_assert(matrix.get_index_for_value(68).col == 2);
return 0;
}
【问题讨论】:
-
我认为这与我报告的 here 相同(或相关)错误。不过,在这种情况下更难解决。
-
@AdrianMole 是的,这是一个错误:developercommunity2.visualstudio.com/t/… 应该先看看那里。
-
我想指出答案属于答案部分。添加“作者的注释”基本上否定了帖子。
-
@StoryTeller-UnslanderMonica 谢谢,我会解决的。
标签: c++ compiler-errors