Я реализую функциональность, которая дает возможность переводить координаты ячеек игрового поля в номер этой ячейки.
Это то, что я пытаюсь (и не могу) заставить работать.
#include <cstdint>
#include <utility>
using UInt32 = std::uint32_t;
template<UInt32... s>
using IndexSequence = std::integer_sequence<UInt32, s...>;
static constexpr UInt32 W = 8;
static constexpr UInt32 H = 8;
template<UInt32 x1, UInt32 x, UInt32 x2, UInt32 y1, UInt32 y2, UInt32... s>
static constexpr auto RegonImpl =
(y1 <= y2)
? (x <= x2)
? RegonImpl<x1, x + 1, x2, y1, y2, s..., W * y1 + x>
: RegonImpl<x1, x1, x2, y1 + 1, y2, s...>
: IndexSequence<s...>{};
template<UInt32 x1, UInt32 x2, UInt32 y1, UInt32 y2>
static constexpr auto Region = RegonImpl<x1, x1, x2, y1, y2>;
int main() {
constexpr auto idx = Region<0, 0, 5, 5>();
}
Ошибка C1202 (слишком сложный контекст зависимости рекурсивного типа или функции) возникает при компиляции.
Вывод ошибки:
... Indexes<8,8>::Region<0,0,1,7>(void) noexcept' being compiled
... Indexes<8,8>::RegionImpl<0,0,0,1,7>' being compiled
... Indexes<8,8>::RegionImpl<1,0,0,1,7,0>' being compiled
... Indexes<8,8>::RegionImpl<2,0,0,1,7,0,1>' being compiled
... Indexes<8,8>::RegionImpl<3,0,0,1,7,0,1,2>' being compiled
... Indexes<8,8>::RegionImpl<4,0,0,1,7,0,1,2,3>' being compiled
... Indexes<8,8>::RegionImpl<5,0,0,1,7,0,1,2,3,4>' being compiled
... Indexes<8,8>::RegionImpl<6,0,0,1,7,0,1,2,3,4,5>' being compiled
... Indexes<8,8>::RegionImpl<7,0,0,1,7,0,1,2,3,4,5,6>' being compiled
... Indexes<8,8>::RegionImpl<8,0,0,1,7,0,1,2,3,4,5,6,7>' being compiled
... Indexes<8,8>::RegionImpl<9,0,0,1,7,0,1,2,3,4,5,6,7,8>' being compiled
...
Как видно из условия x <= x2
всегда верно, что не должно быть.
Я попытался реализовать эту функцию следующим образом:
template<UInt32... s, UInt32... t>
constexpr auto concat(IndexSequence<s...>, IndexSequence<t...>) noexcept {
return IndexSequence<s..., t...>{};
}
template<UInt32 x, UInt32 x1, UInt32 y1, UInt32 x2, UInt32 y2>
static constexpr auto RegionImpl() noexcept {
if constexpr (y1 <= y2) {
if constexpr (x <= x2) {
return concat(IndexSequence<W * y1 + x>{}, RegionImpl<x + 1, x1, y1, x2, y2>());
} else {
return RegionImpl<x1, x1, y1 + 1, x2, y2>();
}
} else {
return IndexSequence<>{};
}
}
template<UInt32 x1, UInt32 y1, UInt32 x2, UInt32 y2>
static constexpr auto Region() noexcept {
return RegionImpl<x1, x1, y1, x2, y2>();
}
Это работает.Но если вместо if statement
использовать conditional operator (a ? b : c)
, то возникает та же ошибка.
Что на самом деле происходит при использовании conditional operator
?