나는 그런 수업이있다
template <typename T>
class Store {
private:
struct pointNode {
T data;
std::unique_ptr<pointNode> next;
pointNode():data(std::move(T{})),next(std::move(std::unique_ptr<pointNode>{})){}
pointNode(T n_data, std::unique_ptr<pointNode> ptr) : data(std::move(n_data)), next(std::move(ptr)) {}
};
std::unique_ptr<pointNode> head;
public:
Store() : head(std::unique_ptr<pointNode>{}) {}
Store(const T& data);
Storek(const T& data, std::unique_ptr<pointNode> ptr);
};
아직까지 unique_ptr과 몇 가지 요소를 생성하지 않았기 때문에 원칙적으로 이러한 호출을 처리하는 클래스로 생성자를 생성 할 수 있는지 이해할 수 없습니다.
Store<int> temp{1,2,3,5};
std :: initializer_list로 뭔가를 시도했습니다.
Store(initializer_list<T> args);
그러나 이것을 unique_ptr에 연결하고 그러한 목록을 만드는 방법을 생각할 수 없습니다. 제 경우에는 아예 불가능할까요? 기꺼이 대답 해 드리겠습니다. 감사합니다
해키 2 줄에 관심이 있다면 :
#include <vector>
#include <memory>
#include <iostream>
template <typename T>
class Store {
private:
struct pointNode {
T data;
std::unique_ptr<pointNode> tail;
pointNode(T n_data, std::unique_ptr<pointNode> ptr) :
data(std::move(n_data)), tail(std::move(ptr)) {}
};
std::unique_ptr<pointNode> head;
public:
Store() : head(std::unique_ptr<pointNode>{}) {}
template<typename...Args>
Store(Args&&...args){
int tmp=0;
// compound assignments are the only right-asociative operators
(tmp+=...+=(head = std::make_unique<pointNode>(std::forward<Args>(args),std::move(head)),0));
// Comma would built reversed list.
// ((head = std::make_unique<pointNode>(std::forward<Args>(args),std::move(head))),...);
}
void print()const {
for(auto ptr=head.get();ptr;ptr=ptr->tail.get()){
std::cout<<ptr->data<<' ';
}
}
};
int main() {
Store<int> s{1,2,3,4,5};
s.print();
}
출력 ( Godbolt ) :
1 2 3 4 5
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다