#include <iostream>
#include <vector>
struct list
{
/**
* Constructor with one argument
*/
explicit list(std::string& arg)
{
size = 1;
parr = new std::string*[1];
helper(0, arg);
}
/**
* Constructor with a variable (>1) number of arguments
* You can use any number of arguments of type std::string in the constructor.
* list(a,b,c,d) = arr;
* list(a,b) = arr;
*/
template <typename ...Args>
list(std::string& arg0, Args&... args)
{
size = sizeof...(Args)+1;
parr = new std::string*[size];
helper(0, arg0, args...);
}
/**
* Assignment operator
* Ref-qualified forbids such things:
* list c(a,b);
* c = arr;
* You can only use this form:
* list(a,b) = arr;
*/
list &operator=(std::vector<std::string> &arr) && // <-- Ref-qualified
{
unsigned int min = (size < arr.size()) ? size : arr.size();
for(unsigned int i = 0; i < min; i++)
{
*parr[i] = arr[i];
}
return *this;
}
/**
* Deleted constructors. Forbids such things:
* list q(a,b,c,d,e),w(a,b,c,d,e);
* w = q;
* You can only use this form:
* list(a,b,c,d,e) = arr;
*/
list(const list &that) = delete;
list(list &&that) = delete;
list() = delete;
~list()
{
delete [] parr;
}
private:
/**
* Helper method.
* Allows to initialize the list of any number of arguments.
* Alternately, one by one makes pointers to the arguments into the internal array.
*/
template <typename ...Args>
void helper(int ind, std::string& arg0, Args&... args)
{
helper(ind, arg0);
helper(++ind, args...);
}
/**
* Helper method.
*/
void helper(int ind, std::string& arg0)
{
parr[ind] = &arg0;
}
// Internal array of pointers to pointers to arguments
std::string **parr;
// The number of arguments with which the constructor was called
unsigned int size;
};
int main(){
std::vector<std::string> arr{"str1","str2","str3","str4","str5","str6"};
std::string a,b,c,d,e;
//list(a,b,c,d,e) = arr; <-- так все работает
(list)(a) = arr; // <-- а так получаю error: ‘a’ has a previous declaration as ‘std::string a’
// The following code forbidden:
// list q(a,b,c,d,e);
// q = arr;
// error: passing ‘list’ as ‘this’ argument of ‘list& list::operator=(std::vector<std::string>&) &&’ discards qualifiers
// The following code forbidden:
// list q(a,b,c,d,e),w(a,b,c,d,e);
// w = q;
// error: use of deleted function ‘list& list::operator=(const list&)’
std::cout << std::endl << a << " " << b << " " << c << " " << d << " " << e << std::endl;
return 0;
};
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKc3RydWN0IGxpc3QKewogICAgLyoqCiAgICAgKiAgQ29uc3RydWN0b3Igd2l0aCBvbmUgYXJndW1lbnQKICAgICAqLwogICAgZXhwbGljaXQgbGlzdChzdGQ6OnN0cmluZyYgYXJnKQogICAgewogICAgICAgIHNpemUgPSAxOwogICAgICAgIHBhcnIgPSBuZXcgc3RkOjpzdHJpbmcqWzFdOwogICAgICAgIGhlbHBlcigwLCBhcmcpOwogICAgfQoKICAgIC8qKgogICAgICogIENvbnN0cnVjdG9yIHdpdGggYSB2YXJpYWJsZSAoPjEpIG51bWJlciBvZiBhcmd1bWVudHMKICAgICAqICAgIFlvdSBjYW4gdXNlIGFueSBudW1iZXIgb2YgYXJndW1lbnRzIG9mIHR5cGUgc3RkOjpzdHJpbmcgaW4gdGhlIGNvbnN0cnVjdG9yLgogICAgICogICAgbGlzdChhLGIsYyxkKSA9IGFycjsKICAgICAqICAgIGxpc3QoYSxiKSA9IGFycjsKICAgICAqLwogICAgdGVtcGxhdGUgPHR5cGVuYW1lIC4uLkFyZ3M+CiAgICBsaXN0KHN0ZDo6c3RyaW5nJiBhcmcwLCBBcmdzJi4uLiBhcmdzKQogICAgewogICAgICAgIHNpemUgPSBzaXplb2YuLi4oQXJncykrMTsKICAgICAgICBwYXJyID0gbmV3IHN0ZDo6c3RyaW5nKltzaXplXTsKICAgICAgICBoZWxwZXIoMCwgYXJnMCwgYXJncy4uLik7CiAgICB9CgogICAgLyoqCiAgICAgKiAgQXNzaWdubWVudCBvcGVyYXRvcgogICAgICogIFJlZi1xdWFsaWZpZWQgZm9yYmlkcyBzdWNoIHRoaW5nczoKICAgICAqICBsaXN0IGMoYSxiKTsKICAgICAqICBjID0gYXJyOwogICAgICogIFlvdSBjYW4gb25seSB1c2UgdGhpcyBmb3JtOgogICAgICogIGxpc3Qo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