#include <utility>
#include <vector>
#include <cstdint>
template <typename T>
struct Base
{
protected:
Base(T&& data):
data(std::forward(data)){
}
virtual ~Base(){};
public:
T getData() const {return data;}
void setData(T&& data) {
this->data = std::forward(data);
}
private:
T data;
};
struct DerivedA: public Base<int>
{
public:
DerivedA(int data):
Base(std::move(data)){//Should I use std::forward instead of std::move here?
}
};
struct DerivedB: public Base<const std::vector<uint16_t> >
{
public:
DerivedB(const std::vector<uint16_t>& data):
Base(std::move(data)){
}
};
My requirements is to have 0 copying of objects when creating the Derived Classes above. But no matter how I write the above I get compiler errors, these are the latest:
bin/Base.h: In instantiation of ‘Base<T>::Base(int, int, int, T&&) [with T = int]’:
bin/Base.h:33:82: required from here
bin/Base.h:12:96: error: no matching function for call to ‘forward(int&)’
/usr/include/c++/4.7/bits/move.h:77:5: note: template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_Tp>::type&)
/usr/include/c++/4.7/bits/move.h:77:5: note: template argument deduction/substitution
What am I doing wrong here?
Also, should I do std::move(data)
when data
in an int
or std::forward
?