Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template function specialization

Reply
Thread Tools

Template function specialization

 
 
davidkevin@o2.pl
Guest
Posts: n/a
 
      07-31-2012
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.



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2012
On 7/31/2012 8:38 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 vector with 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.


It doesn't matter what the vector *of*. If v1 and v2 are both Vector<U>
for some given U type, then

swap(v1, v2)

will invoke the specialized version of the 'swap' instead of the general
one that uses a temporary variable.

> 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 version of swap which was defined in the
> general template.


Yes, but it doesn't create any temporary Vector objects, and doesn't use
assignment of those. It uses the swap for dealing with the built-in
types for the members 'v' and 'size' (probably a pointer and an integer)

> Where is a gap in the above reasoning?


Well, try without the specialization, and see how your program runs, how
many vectors it creates, how many copies it makes. Then provide the
specialization and see if there's any difference. You may see the
light, or you may not.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      07-31-2012
On Tue, 31 Jul 2012 05:38:58 -0700, davidkevin wrote:

> 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 version of swap which was defined in the general template.


The generic swap() calls Vector<T>:perator=, which copies the vector's
data (deep copy), while the specialised version only copies the pointer
(shallow copy) and the size.

 
Reply With Quote
 
davidkevin@o2.pl
Guest
Posts: n/a
 
      07-31-2012
Yes... it is so simple . Thanks!
 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      07-31-2012
On 31/07/2012 19:49, (E-Mail Removed) wrote:
> Yes... it is so simple . Thanks!


In C++11 it is efficient and simpler at the same time, since you no
longer need to implement template specializations to "move" the values
of your objects. In facts, given a type T supporting move semantics, you
can swap two l-values this way:

template <class T> void swap(T& a, T& b) {
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
specialization of a template function in a template class?!?!? pookiebearbottom@yahoo.com C++ 2 08-07-2006 01:08 PM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
Specialization of member function template in template class? Joseph Turian C++ 4 03-20-2006 10:07 AM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments