mem-fn.cc 1.4 KB

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