mem-fn.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file mem-fn.cc
  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 <numeric>
  10. #include <iostream>
  11. #include <iterator>
  12. #include "ra/test.hh"
  13. #include "ra/complex.hh"
  14. using std::cout, std::endl, std::flush, ra::TestRecorder;
  15. using real = double;
  16. struct example
  17. {
  18. float b;
  19. float & f() { return b; }
  20. };
  21. int main()
  22. {
  23. TestRecorder tr;
  24. tr.section("pointer to member");
  25. {
  26. ra::Big<example, 2> ex({2, 3}, ra::scalar(example {99}));
  27. map(std::mem_fn(&example::b), ex) = ra::_0-ra::_1;
  28. tr.test_eq(0, ex(0, 0).b);
  29. tr.test_eq(-1, ex(0, 1).b);
  30. tr.test_eq(-2, ex(0, 2).b);
  31. tr.test_eq(1, ex(1, 0).b);
  32. tr.test_eq(0, ex(1, 1).b);
  33. tr.test_eq(-1, ex(1, 2).b);
  34. map(std::mem_fn(&example::f), ex) = ra::_1-ra::_0;
  35. tr.test_eq(0, ex(0, 0).b);
  36. tr.test_eq(1, ex(0, 1).b);
  37. tr.test_eq(2, ex(0, 2).b);
  38. tr.test_eq(-1, ex(1, 0).b);
  39. tr.test_eq(0, ex(1, 1).b);
  40. tr.test_eq(1, ex(1, 2).b);
  41. }
  42. return tr.summary();
  43. }