fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. /**
  6.  * This is a helper class.
  7.  * It can be used only inside the function ListInitializer list(Args&& ...args)
  8.  * ListInitializer list(Args&& ...args)
  9.  * In all other cases, use it not possible
  10.  */
  11. class ListInitializer
  12. {
  13. public:
  14.  
  15. /**
  16.   * Assignment operator
  17.   * Ref-qualified forbids such things:
  18.   * ListInitializer c(a,b);
  19.   * c = arr;
  20.   * You can only use this form:
  21.   * ListInitializer(a,b) = arr;
  22.   */
  23. ListInitializer &operator=(std::vector<std::string> &arr) && // <-- Ref-qualified
  24. {
  25. unsigned int min = (size < arr.size()) ? size : arr.size();
  26. for(unsigned int i = 0; i < min; i++)
  27. {
  28. *parr[i] = arr[i];
  29. }
  30. }
  31.  
  32. /**
  33.   * Deleted constructors. Forbids such things:
  34.   * list q(a,b,c,d,e),w(a,b,c,d,e);
  35.   * w = q;
  36.   * You can only use this form:
  37.   * list(a,b,c,d,e) = arr;
  38.   */
  39. ListInitializer(const ListInitializer &that) = delete;
  40. ListInitializer() = delete;
  41.  
  42. ~ListInitializer()
  43. {
  44. if(parr) delete [] parr;
  45. }
  46.  
  47. private:
  48. /**
  49.   * Constructor with one argument
  50.   */
  51. explicit ListInitializer(std::string& arg)
  52. {
  53. size = 1;
  54. parr = new std::string*[1];
  55. helper(0, arg);
  56. }
  57.  
  58. /**
  59.   * Constructor with a variable (>1) number of arguments
  60.   * You can use any number of arguments of type std::string in the constructor.
  61. * list(a,b,c,d) = arr;
  62. * list(a,b) = arr;
  63.   */
  64. template <typename ...Args>
  65. ListInitializer(std::string& arg0, Args&... args)
  66. {
  67. size = sizeof...(Args)+1;
  68. parr = new std::string*[size];
  69. helper(0, arg0, args...);
  70. }
  71.  
  72. private:
  73.  
  74. /**
  75.   * Move constructor
  76.   */
  77. ListInitializer(ListInitializer &&that) : parr(that.parr),size(that.size)
  78. {
  79. that.parr = nullptr;
  80. }
  81.  
  82. /**
  83.   * Helper method.
  84.   * Allows to initialize the list of any number of arguments.
  85. * Alternately, one by one makes pointers to the arguments into the internal array.
  86.   */
  87. template <typename ...Args>
  88. void helper(int ind, std::string& arg0, Args&... args)
  89. {
  90. helper(ind, arg0);
  91. helper(++ind, args...);
  92. }
  93.  
  94. /**
  95.   * Helper method.
  96.   */
  97. void helper(int ind, std::string& arg0)
  98. {
  99. parr[ind] = &arg0;
  100. }
  101.  
  102. template <typename ...Args>
  103. friend ListInitializer list(Args& ...args);
  104.  
  105. // Internal array of pointers to pointers to arguments
  106. std::string **parr;
  107. // The number of arguments with which the constructor was called
  108. unsigned int size;
  109. };
  110.  
  111.  
  112. template <typename ...Args>
  113. ListInitializer list(Args& ...args)
  114. {
  115. return ListInitializer(args...);
  116. }
  117.  
  118.  
  119. int main(){
  120.  
  121. std::vector<std::string> arr{"str1","str2","str3","str4","str5","str6"};
  122. std::string a,b,c,d,e;
  123.  
  124. list(b) = arr;
  125. std::cout << std::endl << a << " " << b << " " << c << " " << d << " " << e << std::endl;
  126.  
  127. list(a,b,c,d,e) = arr;
  128. std::cout << std::endl << a << " " << b << " " << c << " " << d << " " << e << std::endl;
  129.  
  130. return 0;
  131. };
  132.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
 str1   

str1 str2 str3 str4 str5