Jar*_*d42 4 c++ compiler-bug constexpr c++14
constexpr std::array我写道,当试图回答问题并创建一个问题时
// Own implementation as std::array::operator[] is not constexpr :/
template <typename T, std::size_t N>
struct array
{
constexpr T& operator[] (std::size_t index) { return data[index];}
constexpr const T& operator[] (std::size_t index) const { return data[index];}
constexpr std::size_t size() const { return N; }
T data[N];
};
constexpr array<std::size_t, 1001u> make_bottle_count()
{
array<std::size_t, 1001u> a = {{0, 1, 2, 3, 4, 1, 2, 1, 2, 3, 1}};
for (int i = 11; i != a.size(); ++i) {
a[i] = 1 + std::min({a[i - 1], a[i - 5], a[i - 7], a[i - 10]});
}
return a;
}
int main() {
// Change constexpr to const make gcc returns expected result
constexpr auto bottle_count = make_bottle_count();
std::cout << bottle_count[17] << std::endl; // expect 2
std::cout << bottle_count[65] << std::endl; // expect 7
std::cout << bottle_count[1000] << std::endl; // expect 100
}
Run Code Online (Sandbox Code Playgroud)
但是gcc给出了意想不到的结果(2 2 2)而clang给出了预期的结果.
删除constexpr(或替换const)bottle_count可获得两者的预期结果.
我调用UB,还是编译器(gcc)错误?
这是一个gcc bug,请参阅:https://gcc.gnu.org/bugzilla/show_bug.cgi?id = 67104
我将把你的测试用例添加到bug中.
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |