boost_no_ops_in_namespace.ipp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // (C) Copyright Jeremy Siek 1999.
  2. // (C) Copyright David Abrahams 1999.
  3. // (C) Copyright John Maddock 2001.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/config for most recent version.
  8. // MACRO: BOOST_NO_OPERATORS_IN_NAMESPACE
  9. // TITLE: friend operators in namespace
  10. // DESCRIPTION: Compiler requires inherited operator
  11. // friend functions to be defined at namespace scope,
  12. // then using'ed to boost.
  13. // Probably GCC specific. See boost/operators.hpp for example.
  14. namespace boost{
  15. //
  16. // the following is taken right out of <boost/operators.hpp>
  17. //
  18. template <class T>
  19. struct addable1
  20. {
  21. friend T operator+(T x, const T& y) { return x += y; }
  22. friend bool operator != (const T& a, const T& b) { return !(a == b); }
  23. };
  24. struct spoiler1
  25. {};
  26. spoiler1 operator+(const spoiler1&,const spoiler1&);
  27. bool operator !=(const spoiler1&, const spoiler1&);
  28. } // namespace boost
  29. namespace boost_no_operators_in_namespace{
  30. struct spoiler2
  31. {};
  32. spoiler2 operator+(const spoiler2&,const spoiler2&);
  33. bool operator !=(const spoiler2&, const spoiler2&);
  34. class add : public boost::addable1<add>
  35. {
  36. int val;
  37. public:
  38. add(int i) { val = i; }
  39. add(const add& a){ val = a.val; }
  40. add& operator+=(const add& a) { val += a.val; return *this; }
  41. bool operator==(const add& a)const { return val == a.val; }
  42. };
  43. int test()
  44. {
  45. add a1(2);
  46. add a2(3);
  47. add a3(0);
  48. a3 = a1 + a2;
  49. bool b1 = (a1 == a2);
  50. b1 = (a1 != a2);
  51. (void)b1;
  52. return 0;
  53. }
  54. }