At the outset, I realize what I did was bad. I relied on what is now (at least) undefined behavior, if not explicitly forbidden. It used to work, and I thought I was being clever. Now it doesn't and I'm trying to fix it.
I have positive power-of-2 numbers (FFT bin index, but not important). I want to effectively FFT-shift a set of bin indices by wrapping the second half of the values to the negative range. That is, given an FFT size of 512,
0 ... 255 -> 0 ... 255
256 ... 511 -> -256 ... -1
What used to work was
template <size_t N>
struct Wrapper {
int val : N;
};
auto constexpr index = 42u;
auto wrapper = Wrapper<9>{ index }; // warning: invalid narrowing conversion from "unsigned int" to "int"
auto val = wrapper.val; // signed value
This relied on the truncation of overflowed assignment, but was empirically tested and Just Worked(tm).
Now, it doesn't compile (cleanly).
How should I perform this conversion now?