fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct list
  5. {
  6. /**
  7.   * Constructor with one argument
  8.   */
  9. explicit list(std::string& arg)
  10. {
  11. size = 1;
  12. parr = new std::string*[1];
  13. helper(0, arg);
  14. }
  15.  
  16. /**
  17.   * Constructor with a variable (>1) number of arguments
  18.   * You can use any number of arguments of type std::string in the constructor.
  19.   * list(a,b,c,d) = arr;
  20.   * list(a,b) = arr;
  21.   */
  22. template <typename ...Args>
  23. list(std::string& arg0, Args&... args)
  24. {
  25. size = sizeof...(Args)+1;
  26. parr = new std::string*[size];
  27. helper(0, arg0, args...);
  28. }
  29.  
  30. /**
  31.   * Assignment operator
  32.   * Ref-qualified forbids such things:
  33.   * list c(a,b);
  34.   * c = arr;
  35.   * You can only use this form:
  36.   * list(a,b) = arr;
  37.   */
  38. list &operator=(std::vector<std::string> &arr) && // <-- Ref-qualified
  39. {
  40. unsigned int min = (size < arr.size()) ? size : arr.size();
  41. for(unsigned int i = 0; i < min; i++)
  42. {
  43. *parr[i] = arr[i];
  44. }
  45. return *this;
  46. }
  47.  
  48. /**
  49.   * Deleted constructors. Forbids such things:
  50.   * list q(a,b,c,d,e),w(a,b,c,d,e);
  51.   * w = q;
  52.   * You can only use this form:
  53.   * list(a,b,c,d,e) = arr;
  54.   */
  55. list(const list &that) = delete;
  56. list(list &&that) = delete;
  57. list() = delete;
  58.  
  59. ~list()
  60. {
  61. delete [] parr;
  62. }
  63.  
  64. private:
  65.  
  66. /**
  67.   * Helper method.
  68.   * Allows to initialize the list of any number of arguments.
  69.   * Alternately, one by one makes pointers to the arguments into the internal array.
  70.   */
  71. template <typename ...Args>
  72. void helper(int ind, std::string& arg0, Args&... args)
  73. {
  74. helper(ind, arg0);
  75. helper(++ind, args...);
  76. }
  77.  
  78. /**
  79.   * Helper method.
  80.   */
  81. void helper(int ind, std::string& arg0)
  82. {
  83. parr[ind] = &arg0;
  84. }
  85.  
  86. // Internal array of pointers to pointers to arguments
  87. std::string **parr;
  88. // The number of arguments with which the constructor was called
  89. unsigned int size;
  90. };
  91.  
  92.  
  93. int main(){
  94.  
  95. std::vector<std::string> arr{"str1","str2","str3","str4","str5","str6"};
  96. std::string a,b,c,d,e;
  97. //list(a,b,c,d,e) = arr; <-- так все работает
  98. (list)(a) = arr; // <-- а так получаю error: ‘a’ has a previous declaration as ‘std::string a’
  99.  
  100. // The following code forbidden:
  101. // list q(a,b,c,d,e);
  102. // q = arr;
  103. // error: passing ‘list’ as ‘this’ argument of ‘list& list::operator=(std::vector<std::string>&) &&’ discards qualifiers
  104.  
  105. // The following code forbidden:
  106. // list q(a,b,c,d,e),w(a,b,c,d,e);
  107. // w = q;
  108. // error: use of deleted function ‘list& list::operator=(const list&)’
  109.  
  110. std::cout << std::endl << a << " " << b << " " << c << " " << d << " " << e << std::endl;
  111.  
  112. return 0;
  113. };
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
str1