fork download
  1. #include <ostream>
  2. #include <iostream>
  3. #include <string>
  4. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  5. #include <boost/fusion/mpl.hpp>
  6. #include <boost/mpl/replace.hpp>
  7. #include <boost/fusion/container.hpp>
  8. #include <boost/fusion/sequence.hpp>
  9. #include <typeinfo>
  10.  
  11. using namespace std;
  12.  
  13. template<class DERIVED_TYPE>
  14. struct HaveChildren
  15. {
  16. friend ostream& operator<< (ostream& streamReceiver, const HaveChildren<DERIVED_TYPE>& streamSender)
  17. {
  18. DERIVED_TYPE const& RealStreamSender = static_cast<DERIVED_TYPE const&>(streamSender);
  19. streamReceiver << RealStreamSender.children;
  20.  
  21. return streamReceiver ;
  22. }
  23. };
  24.  
  25. template<typename... CHILDREN_TYPES>
  26. struct childrenListType : public boost::fusion::vector<CHILDREN_TYPES...>
  27. {
  28. childrenListType(CHILDREN_TYPES&&... args) : boost::fusion::vector<CHILDREN_TYPES...>(forward<CHILDREN_TYPES>(args)...) {}
  29.  
  30. friend ostream& operator<< (ostream& streamReceiver, const childrenListType<CHILDREN_TYPES...>& streamSender)
  31. {
  32. for_each(streamSender, [&](const auto& child)
  33. {
  34. streamReceiver << child;
  35. });
  36.  
  37. return streamReceiver;
  38. }
  39. };
  40.  
  41. struct stringAndSpace :HaveChildren<stringAndSpace>
  42. {
  43. struct space
  44. {
  45. space() {}
  46. friend ostream& operator<< (ostream& streamReceiver, const space& streamSender)
  47. {
  48. return streamReceiver << " ";
  49. }
  50. };
  51.  
  52. const childrenListType<string, space> children;
  53.  
  54. template<typename STRING_VALUE_TYPE>
  55. stringAndSpace(STRING_VALUE_TYPE&& value) :children(std::forward<STRING_VALUE_TYPE >(value), {}) {}
  56. };
  57.  
  58. struct MyChildrenNeedsSpaceWithReplacer : HaveChildren<MyChildrenNeedsSpaceWithReplacer>
  59. {
  60. typedef childrenListType<string, string, string, string> context;
  61. typedef boost::mpl::replace< context, string, stringAndSpace >::type replacedContext;
  62.  
  63. const replacedContext children;
  64.  
  65. MyChildrenNeedsSpaceWithReplacer() : children( "this" ,"sentence" , "needs" , "spaces")
  66. {
  67. std::cout << endl << "The children type is:" << endl <<typeid(children).name() << endl;
  68. }
  69. };
  70.  
  71. struct MyChildrenNeedsSpace : HaveChildren<MyChildrenNeedsSpace>
  72. {
  73. typedef childrenListType<string, string, string, string> context;
  74.  
  75. const context children;
  76.  
  77. MyChildrenNeedsSpace() : children("this", "sentence", "needs", "spaces")
  78. {
  79. std::cout << endl << "The children type is:" << endl << typeid(children).name() << endl;
  80. }
  81. };
  82.  
  83. int main(int argc, char* argv[])
  84. {
  85. cout << "First we output the class where the strings do not get replaced with strings AND spaces" << endl;
  86. cout << "Output:"<< endl << MyChildrenNeedsSpace () ;
  87. cout << endl << endl << "Now we output the class where I try to inject spaces between the words. " << endl;
  88. cout << "Output:" <<endl << MyChildrenNeedsSpaceWithReplacer();
  89. int i; cin >> i;
  90. return 0;
  91. }
  92. ;
Success #stdin #stdout 0s 3240KB
stdin
Standard input is empty
stdout
First we output the class where the strings do not get replaced with strings AND spaces

The children type is:
16childrenListTypeIISsSsSsSsEE
Output:
thissentenceneedsspaces

Now we output the class where I try to inject spaces between the words. 

The children type is:
N5boost6fusion7vector4I14stringAndSpaceS2_S2_S2_EE
Output:
(this  sentence  needs  spaces )