D
davidkevin
Hi,
Stroustrup has shown a following sample of the template function specialization in his book "The C++ Programming Language":
template<class T> class Vector {
/*...*/
void swap(Vector&);
/*...*/
}
template<class T> void swap(T&x, T& y)
{
T t = x;
x = y;
y = t;
}
Here he has stated that:
code which is quoted above will work but will be ineffective if executed for a vector of vectors. A reason is that function swap() replaces one vectorwith other by copying elements. An object whose type is Vector has enough data to give an indirect access to elements. So it is possible to make a new design of definition such that representations will be swapped. To make it possible to deal with the representation, in class Vector there is a method swap:
template<class T> void Vector<T>::swap(Vector& a)
{
swap(v, a.v);
swap(size, a.size);
}
Then he has said that:
It is possible to use a method swap to define a specialization of general swap() function.
template<class T> void swap(Vector<T>& a, Vector<T>& b)
{
a.swap(b);
}
I am sure I am missing something but I can't see how above code make it possible to achieve a better efficiency when swap() will be called for a vector of vectors.
The last piece of code makes that if swap will be executed for parameters which are both Vectors then a special version of that method will be called (a.swap(b) - that is method which is defined in Vector). But when we look how Vector<T>::Swap is defined, we can see that it simply calls that versionof swap which was defined in the general template.
Where is a gap in the above reasoning?
Thanks in advance for responses,
Greetings.
Stroustrup has shown a following sample of the template function specialization in his book "The C++ Programming Language":
template<class T> class Vector {
/*...*/
void swap(Vector&);
/*...*/
}
template<class T> void swap(T&x, T& y)
{
T t = x;
x = y;
y = t;
}
Here he has stated that:
code which is quoted above will work but will be ineffective if executed for a vector of vectors. A reason is that function swap() replaces one vectorwith other by copying elements. An object whose type is Vector has enough data to give an indirect access to elements. So it is possible to make a new design of definition such that representations will be swapped. To make it possible to deal with the representation, in class Vector there is a method swap:
template<class T> void Vector<T>::swap(Vector& a)
{
swap(v, a.v);
swap(size, a.size);
}
Then he has said that:
It is possible to use a method swap to define a specialization of general swap() function.
template<class T> void swap(Vector<T>& a, Vector<T>& b)
{
a.swap(b);
}
I am sure I am missing something but I can't see how above code make it possible to achieve a better efficiency when swap() will be called for a vector of vectors.
The last piece of code makes that if swap will be executed for parameters which are both Vectors then a special version of that method will be called (a.swap(b) - that is method which is defined in Vector). But when we look how Vector<T>::Swap is defined, we can see that it simply calls that versionof swap which was defined in the general template.
Where is a gap in the above reasoning?
Thanks in advance for responses,
Greetings.