3

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?

Community
  • 1
  • 1
EqualKirby
  • 33
  • 3
  • 1
    possible duplicate of [Template Constructor Taking Precedence Over Normal Copy and Move Constructor?](http://stackoverflow.com/questions/14100171/template-constructor-taking-precedence-over-normal-copy-and-move-constructor) – n. m. could be an AI Sep 08 '13 at 15:16

1 Answers1

1

I don't really see the need for the constructors itself to also be templates, see below. Also, consider using 'std::move' in the move constructor when initializing tuple member.

template<typename... Dummy>
struct tuple;

template <typename T, typename... TList>
struct tuple<T,TList...>
  :public tuple<TList...>
{
  typedef T value_type;

  tuple(){}

  tuple(const T& a,const TList&... args)
    :tuple<TList...>(args...),element(a)
  {
  }

  tuple(T&& a,TList&&... 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 int size=1+tuple<TList...>::size;
  static const int offset=sizeof(tuple<TList...>);

  T element;
};

template<typename T>
struct tuple<T>
{
  typedef T element_type;

  tuple(){}

  tuple(const T& a)
    :element(a)
  {
  }

  tuple(T&& a)
    :element(a)
  {       
  }

  tuple(const tuple<T>& a)
    :element(a.element)
  {
  }

  tuple(tuple<T>&& a)
    :element(a.element)
  {
  }

  static const int size=1;
  static const int offset=0;

  T element;
};

int
main ()
{
  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.
}
chill
  • 16,470
  • 2
  • 40
  • 44