У меня проблема с моим кодом, связанным с компилятором, требующим, чтобы функция-член была постоянной. Я не уверен, как заставить его работать, так как мне нужно изменить m_data
в функции get, и поэтому он не может быть постоянной функцией-членом.
Вот код
#include <array>
#include <cstdint>
#include <cstdlib>
template <typename t, const size_t size, const size_t key>
class XorArray {
public:
// holds encrypted array data
std::array<t, size> m_data;
// compile-time create xor array
__attribute__((always_inline)) constexpr XorArray(
const std::array<t, size> &elements)
: m_data{} {
for (size_t i = 0; i < size; ++i) m_data[i] = elements[i] ^ key;
}
// retrive unxored data at runtime
__attribute__((always_inline)) const t *get(){
for (auto &element : m_data) element ^= key;
return m_data.data();
}
};
static constexpr std::array<uint8_t, 5> test_data = {68, 88, 66, 67, 80};
int main() {
static constexpr auto test_data_enc =
XorArray<uint8_t, test_data.size(), 0x5>(test_data);
const auto test_get = test_data_enc.get();
return 0;
}
Сообщение об ошибке:
error: passing 'const XorArray<unsigned char, 5, 5>' as 'this' argument discards qualifiers [-fpermissive]
const auto test_get = test_data_enc.get();
демо