Stos powinien być tylko adapterem. Takim, jak ten teraz machnięty:
#include <iostream>
#include <deque>
using namespace std;
template<
typename T,
class Container = deque<T>
> class stack{
public:
using value_type = T;
using container_type = Container;
using reference = typename container_type::reference;
using const_reference = typename container_type::const_reference;
using size_type = ::size_t;
stack(const container_type &container_): container(container_){}
stack(container_type &&container_ = container_type()): container(container_){}
bool empty() const{
return container.empty();
}
size_type size() const{
return container.size();
}
reference &top(){
return container.back();
}
const reference &top() const{
return container.back();
}
void push(const value_type &val){
container.push_back(val);
}
void push(value_type &&val){
container.push_back(val);
}
template <class... Args>
void emplace(Args &&...args){
container.emplace_back(args...);
}
void pop(){
container.pop_back();
}
private:
container_type container;
};
template<typename T>
struct push_many_proxy{
using pushable_type = T;
using value_type = typename pushable_type::value_type;
pushable_type &pushable;
push_many_proxy(pushable_type &pushable_): pushable(pushable_){}
push_many_proxy &operator()(const value_type &val){
pushable.push(val);
return *this;
}
push_many_proxy &operator()(value_type &&val){
pushable.push(val);
return *this;
}
};
template<typename T>
push_many_proxy<T> push_many(T &pushable){
return push_many_proxy<T>(pushable);
}
int main(){
using nums_stack = stack<int>;
nums_stack nums;
push_many(nums)(1)(2)(3)(4)(5);
while(!nums.empty()){
cout << nums.top();
nums.pop();
}
return 0;
}
Dodałem drobny smaczek w nawiązaniu do tego wątku: http://4programmers.net/Forum/C_i_C++/255088-boost_dziwna_konstrukcja
uruchomienie(na ideone)</code> | Wyjście: <code class="python">54321
Oczywiście, gdyby takie proxy miałoby być więcej używane, powinno raczej używać jakiegoś insertera:
template<typename Inserter>
struct insert_many_proxy{
using inserter_type = Inserter;
inserter_type inserter;
insert_many_proxy(inserter_type inserter_): inserter(inserter_){}
template<typename T>
insert_many_proxy &operator()(const T &val){
inserter = val;
return *this;
}
template<typename T>
insert_many_proxy &operator()(T &&val){
inserter = val;
return *this;
}
};
template<typename Inserter>
insert_many_proxy<Inserter> insert_many(Inserter inserter){
return insert_many_proxy<Inserter>(inserter);
}