mem-fn.C 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file mem-fn.C
  3. /// @brief Using map with pointers to members. Until uniform call syntax is a thing.
  4. // (c) Daniel Llorens - 2018-2019
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include <iterator>
  11. #include <numeric>
  12. #include "ra/complex.H"
  13. #include "ra/test.H"
  14. #include "ra/big.H"
  15. #include "ra/operators.H"
  16. #include "ra/io.H"
  17. using std::cout, std::endl, std::flush;
  18. using real = double;
  19. struct example
  20. {
  21. float b;
  22. float & f() { return b; }
  23. };
  24. int main()
  25. {
  26. TestRecorder tr;
  27. tr.section("pointer to member");
  28. {
  29. ra::Big<example, 2> ex({2, 3}, ra::scalar(example {99}));
  30. map(std::mem_fn(&example::b), ex) = ra::_0-ra::_1;
  31. tr.test_eq(0, ex(0, 0).b);
  32. tr.test_eq(-1, ex(0, 1).b);
  33. tr.test_eq(-2, ex(0, 2).b);
  34. tr.test_eq(1, ex(1, 0).b);
  35. tr.test_eq(0, ex(1, 1).b);
  36. tr.test_eq(-1, ex(1, 2).b);
  37. map(std::mem_fn(&example::f), ex) = ra::_1-ra::_0;
  38. tr.test_eq(0, ex(0, 0).b);
  39. tr.test_eq(1, ex(0, 1).b);
  40. tr.test_eq(2, ex(0, 2).b);
  41. tr.test_eq(-1, ex(1, 0).b);
  42. tr.test_eq(0, ex(1, 1).b);
  43. tr.test_eq(1, ex(1, 2).b);
  44. }
  45. return tr.summary();
  46. }