fork download
  1. #include <cstddef>
  2. #include <cstdint>
  3. #include <iostream>
  4.  
  5. template<size_t bytesLeft>
  6. void doXor(void *p1, void *p2) {
  7. if constexpr(bytesLeft >= 8) {
  8. *reinterpret_cast<uint64_t*>(p1) ^= *reinterpret_cast<uint64_t*>(p2);
  9. doXor<bytesLeft - 8>(reinterpret_cast<uint8_t*>(p1) + 8, reinterpret_cast<uint8_t*>(p2) + 8);
  10. } else if constexpr(bytesLeft >= 4) {
  11. *reinterpret_cast<uint32_t*>(p1) ^= *reinterpret_cast<uint32_t*>(p2);
  12. doXor<bytesLeft - 4>(reinterpret_cast<uint8_t*>(p1) + 4, reinterpret_cast<uint8_t*>(p2) + 4);
  13. } else if constexpr(bytesLeft != 0) {
  14. *reinterpret_cast<uint8_t*>(p1) ^= *reinterpret_cast<uint8_t*>(p2);
  15. doXor<bytesLeft - 1>(reinterpret_cast<uint8_t*>(p1) + 1, reinterpret_cast<uint8_t*>(p2) + 1);
  16. }
  17. }
  18.  
  19. template<typename T>
  20. T& operator^=(T& a, T& b) {
  21. doXor<sizeof(T)>(&a, &b);
  22. return a;
  23. }
  24.  
  25. struct A {
  26. uint64_t a;
  27. uint64_t b;
  28. uint64_t c;
  29. uint64_t d;
  30. };
  31.  
  32. int main() {
  33. A a{1,2,3,4};
  34. A b{5,6,7,8};
  35.  
  36. std::cout << a.a << " " << a.b << " " << a.c << " " << a.d << std::endl;
  37. std::cout << b.a << " " << b.b << " " << b.c << " " << b.d << std::endl;
  38.  
  39. a ^= b;
  40. b ^= a;
  41. a ^= b;
  42.  
  43. std::cout << a.a << " " << a.b << " " << a.c << " " << a.d << std::endl;
  44. std::cout << b.a << " " << b.b << " " << b.c << " " << b.d << std::endl;
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void doXor(void*, void*)’:
prog.cpp:7:5: error: expected ‘(’ before ‘constexpr’
  if constexpr(bytesLeft >= 8) {
     ^~~~~~~~~
prog.cpp:10:4: error: ‘else’ without a previous ‘if’
  } else if constexpr(bytesLeft >= 4) {
    ^~~~
prog.cpp:10:12: error: expected ‘(’ before ‘constexpr’
  } else if constexpr(bytesLeft >= 4) {
            ^~~~~~~~~
prog.cpp:13:4: error: ‘else’ without a previous ‘if’
  } else if constexpr(bytesLeft != 0) {
    ^~~~
prog.cpp:13:12: error: expected ‘(’ before ‘constexpr’
  } else if constexpr(bytesLeft != 0) {
            ^~~~~~~~~
stdout
Standard output is empty