STL question. what is the difference...

M

Michal

Hallo Group Members

I unfortunatelly do not feel the difference between the 2 below.

namespace std {
template<typename T>
void swap(Widget<T>& a,
Widget<T>& b)
{ .... }
}


namespace std {
template<typename T>
void swap<Widget<T> >(Widget<T>& a,
Widget<T>& b)
{ .... }
}

Can You tell me what is the one?

best regards,
Michal
 
V

Victor Bazarov

Michal said:
I unfortunatelly do not feel the difference between the 2 below.

namespace std {
template<typename T>
void swap(Widget<T>& a,
Widget<T>& b)
{ .... }
}


namespace std {
template<typename T>
void swap<Widget<T> >(Widget<T>& a,
Widget<T>& b)
{ .... }
}

Can You tell me what is the one?

The former is the declaration and definition of the 'swap' template
function, the other is an attempt (currently illegal) to partially
specialise the existing template.

V
 
A

Andrey Tarasevich

Michal said:
I unfortunatelly do not feel the difference between the 2 below.

namespace std {
template<typename T>
void swap(Widget<T>& a,
Widget<T>& b)
{ .... }
}


namespace std {
template<typename T>
void swap<Widget<T> >(Widget<T>& a,
Widget<T>& b)
{ .... }
}

Can You tell me what is the one?

The second is not valid C++. Taking that into account, it is hard to
have a meaningful discussion about any other "differences" between the two.
 
M

Michal

Victor Bazarov said:
The former is the declaration and definition of the 'swap' template
function, the other is an attempt (currently illegal) to partially
specialise the existing template.

V


conclusion: so if there is <....> after function name, then can I read
it as "specialization" (partial or total - whatever)?
 
L

litb

conclusion: so if there is <....> after function name, then can I read
it as "specialization" (partial or total - whatever)?

Yes. The result of an instantiation of a template is a function or a
class - depending on whether it was a function template or a class
template. That result is called specialization, because it's
specialized for that particular template parameter values. Sometimes
you write explicit specializations, if you don't want the compiler to
generate one for you out of the primary template (that generation
process is called instantiation) - for example because you want some
special handling to take place.

If you read <....> after a function name, it always refers to or
declares a specialization. Sometimes, it will cause instantiation of a
specialization from a primary template, and other times that
particular use declares an explicit specialization.
 
M

Michal

litb said:
If you read <....> after a function name, it always refers to or
declares a specialization. Sometimes, it will cause instantiation of a
specialization from a primary template, and other times that
particular use declares an explicit specialization.

.... and what if I wrote it this way:
namespace std {
template<typename T>
void swap(Widget<T>& a,
Widget<T>& b)
{ .... }
}

Should it be read as some kind of "swap" function overloading, but
definitelly not any specialization or instantiation?

best regards,
Michal
 
A

Andrey Tarasevich

Michal said:
... and what if I wrote it this way:
namespace std {
template<typename T>
void swap(Widget<T>& a,
Widget<T>& b)
{ .... }
}

Should it be read as some kind of "swap" function overloading, but
definitelly not any specialization or instantiation?

This is indeed overloading, not a specialization or instantiation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top