• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
6 // Use, modification and
7 // distribution is subject to the Boost Software License, Version
8 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see http://www.boost.org
11 
12 #include <boost/shared_ptr.hpp>
13 #include <boost/signals2/deconstruct.hpp>
14 #include <iostream>
15 
16 namespace bs2 = boost::signals2;
17 
18 namespace mynamespace
19 {
20   class X
21   {
22   public:
23     /* This adl_postconstruct function will be found
24     via argument-dependent lookup when using boost::signals2::deconstruct. */
25     template<typename T> friend
adl_postconstruct(const boost::shared_ptr<T> &,X *)26       void adl_postconstruct(const boost::shared_ptr<T> &, X *)
27     {
28       std::cout << "world!" << std::endl;
29     }
30   private:
31     friend class bs2::deconstruct_access;  // give boost::signals2::deconstruct access to private constructor
32     // private constructor forces use of boost::signals2::deconstruct to create objects.
X()33     X()
34     {
35       std::cout << "Hello, ";
36     }
37   };
38 }
39 
main()40 int main()
41 {
42   // adl_postconstruct will be called during implicit conversion of return value to shared_ptr
43   boost::shared_ptr<mynamespace::X> x = bs2::deconstruct<mynamespace::X>();
44   return 0;
45 }
46