Here's the code I have, as a background:
template<typename... Dummy>
struct tuple;
template <typename T, typename... TList>
struct tuple<T,TList...>
:public tuple<TList...>
{
typedef T value_type;
tuple(){}
template<typename A,typename... AList>
tuple(const A& a,const AList&... args)
:tuple<TList...>(args...),element(a)
{
}
template<typename A,typename... AList>
tuple(A&& a,AList&&... args)
:tuple<TList...>(args...),element(a)
{
}
tuple(const tuple<T,TList...>& a)
:tuple<TList...>(a),element(a.element)
{
}
tuple(tuple<T,TList...>&& a)
:tuple<TList...>(a),element(a.element)
{
}
static const index_t size=1+tuple<TList...>::size;
static const index_t offset=sizeof(tuple<TList...>);
tuple_unit<T> element;
};
template<typename T>
struct tuple<T>
{
typedef T element_type;
tuple(){}
template<typename A>
tuple(const A& a)
:element(a)
{
}
template<typename A>
tuple(A&& a)
:element(a)
{
}
tuple(const tuple<T>& a)
:element(a.element)
{
}
tuple(tuple<T>&& a)
:element(a.element)
{
}
static const index_t size=1;
static const index_t offset=0;
tuple_unit<T> element;
};
Since in C++11 we have move semantics, I tried to add move constructor in my project. But the result isn't as my expected.
When I write
tuple<int,float,double> a(3,5,7); //Success. Use 3,5,7 to init each element in tuple.
tuple<int,float,double> b(a); //Call template function,not copy ctor,and it failed.
I read this, and it made me think that b(a)
will call template<typename A,typename... AList>
tuple(A&& a,AList&&... args)
, and A& &&
will be replaced with A&
, but I already have a constructor tuple(const tuple<T>& a)
. Why will the compiler think the template constructor is better than copy constructor?
What should I do to solve the problem?