任何人都知道为什么转发复制ctor不起作用?
关于一种类型是向量而另一种类型是堆栈的编译器错误???
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <type_traits>
using namespace std;
template<class T>
class stack{
public:
stack();
template<class X>
stack(X&& other);
template<typename X>
void push(X&& element);
T const & top() const;
void pop();
inline std::size_t const size() const;
typedef std::vector<T> vector_type;
private:
std::vector<T> elements;
};
template<class T>
stack<T>::stack(){}
template<class T>
template<class X>
stack<T>::stack(X&& other) : elements(std::forward<typename X>(other.elements)){
}
template<class T>
template<class X>
void stack<T>::push(X&& element){
this->elements.push_back(std::forward<X>(element));
}
template<class T>
T const & stack<T>::top() const{
return this->elements[this->elements.size() -1];
}
template<class T>
inline std::size_t const stack<T>::size() const{
return this->elements.size();
}
template<class T>
void stack<T>::pop(){
this->elements.pop_back();
}
template<class T,int N>
stack<T> get_stack_of_n_defaults(){
stack<T> s;
for(int i = 0; i < N; ++i){
s.push( T()); // should pass by rvalue ref;
}
return s;
}
int main()
{
stack<int> s = get_stack_of_n_defaults<int,5>();
for(int i = 0; i <= 10; ++i){
s.push(i);
}
cout << s.size() << endl;
for(int i = 0; i <= 10; ++i){
cout << s.top() << endl;;
s.pop();
}
cout << s.size() << endl;
return 0;
}