boost_no_ptr_mem_const.ipp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2000
  2. // Cadenza New Zealand Ltd
  3. //
  4. // (C) Copyright John Maddock 2001.
  5. //
  6. // Use, modification and distribution are subject to the
  7. // Boost Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. // See http://www.boost.org/libs/config for the most recent version.
  10. // MACRO: BOOST_NO_POINTER_TO_MEMBER_CONST
  11. // TITLE: pointers to const member functions
  12. // DESCRIPTION: The compiler does not correctly handle
  13. // pointers to const member functions, preventing use
  14. // of these in overloaded function templates.
  15. // See boost/functional.hpp for example.
  16. namespace boost_no_pointer_to_member_const{
  17. template <class S, class T>
  18. class const_mem_fun_t
  19. {
  20. public:
  21. explicit const_mem_fun_t(S (T::*p)() const)
  22. :
  23. ptr(p)
  24. {}
  25. S operator()(const T* p) const
  26. {
  27. return (p->*ptr)();
  28. }
  29. private:
  30. S (T::*ptr)() const;
  31. };
  32. template <class S, class T, class A>
  33. class const_mem_fun1_t
  34. {
  35. public:
  36. explicit const_mem_fun1_t(S (T::*p)(A) const)
  37. :
  38. ptr(p)
  39. {}
  40. S operator()(const T* p, const A& x) const
  41. {
  42. return (p->*ptr)(x);
  43. }
  44. private:
  45. S (T::*ptr)(A) const;
  46. };
  47. template<class S, class T>
  48. inline const_mem_fun_t<S,T> mem_fun(S (T::*f)() const)
  49. {
  50. return const_mem_fun_t<S,T>(f);
  51. }
  52. template<class S, class T, class A>
  53. inline const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const)
  54. {
  55. return const_mem_fun1_t<S,T,A>(f);
  56. }
  57. class tester
  58. {
  59. public:
  60. void foo1()const{}
  61. int foo2(int i)const{ return i*2; }
  62. };
  63. int test()
  64. {
  65. boost_no_pointer_to_member_const::mem_fun(&tester::foo1);
  66. boost_no_pointer_to_member_const::mem_fun(&tester::foo2);
  67. return 0;
  68. }
  69. }