postconstructor_ex1.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Minimal example of defining a postconstructor for a class which
  2. // uses boost::signals2::deconstruct as its factory function.
  3. //
  4. // Copyright Frank Mori Hess 2009.
  5. // Use, modification and
  6. // distribution is subject to the Boost Software License, Version
  7. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // For more information, see http://www.boost.org
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/signals2/deconstruct.hpp>
  12. #include <iostream>
  13. namespace bs2 = boost::signals2;
  14. namespace mynamespace
  15. {
  16. class X
  17. {
  18. public:
  19. /* This adl_postconstruct function will be found
  20. via argument-dependent lookup when using boost::signals2::deconstruct. */
  21. template<typename T> friend
  22. void adl_postconstruct(const boost::shared_ptr<T> &, X *)
  23. {
  24. std::cout << "world!" << std::endl;
  25. }
  26. private:
  27. friend class bs2::deconstruct_access; // give boost::signals2::deconstruct access to private constructor
  28. // private constructor forces use of boost::signals2::deconstruct to create objects.
  29. X()
  30. {
  31. std::cout << "Hello, ";
  32. }
  33. };
  34. }
  35. int main()
  36. {
  37. // adl_postconstruct will be called during implicit conversion of return value to shared_ptr
  38. boost::shared_ptr<mynamespace::X> x = bs2::deconstruct<mynamespace::X>();
  39. return 0;
  40. }