1

I have some code looking like this

#include <array>

constexpr std::size_t N = 16;

template<typename Array>
constexpr void takeparam(Array& data, size_t I) {
    if  (I < N - 2) { // <- fails if I add constexpr
       data[I] = data[I + 2];
    }
}

template<size_t I, typename Array>
constexpr void taketemplate(Array& data) {
    if  constexpr (I < N - 2) { // <- here constexpr is OK
       data[I] = data[I + 2];
    }
}

int main() {
    std::array<int, 16> data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

    takeparam(data, 3); 
    taketemplate<3>(data); 
}

I was expecting to be able to use if constexpr in the takeparam function, but I cannot get it work. What am I doing wrong?

OznOg
  • 4,440
  • 2
  • 26
  • 35

0 Answers0