Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   specialization or overload (http://www.velocityreviews.com/forums/t754626-specialization-or-overload.html)

nguillot 10-04-2011 05:10 PM

specialization or overload
 
Hello.

I would like to specialize a templated function for the type
std::vector<U>, U being a typename.
I think it is not possible.

However I read on forum we can (I describe just after how) but I guess
this is not specialization but overload.

I’ve got 2 questions:
1) Is it specialization or overload?
2) In fact, if it works, I don’t really care the answer of this
question 1 (execpt for my knowledge): does the standard assure it
works (resolution order) ?

Now the stuff:

Let’s have the template function:

template <typename T>
void f(const T& p_value)
{
cout << "generic" << endl;
}

The specialization (for bool for instance) is

template <>
void f<bool>(const bool& p_value)
{
cout << "bool" << endl;
}

The "specialization" for vector<U> is (or is it an overload?)

template <typename U>
void f(const std::vector<U> & p_value)
{
cout << "vector" << endl;
}

And indeed if I do :

int i;
vector<double> v;
bool b;

f(i);
f(v);
f(b);

I get:

generic
vector
bool

So, is it really specialization?
If no, am I sure f(v) will always calls this "specialization?

Thanks in advance.

Victor Bazarov 10-04-2011 06:16 PM

Re: specialization or overload
 
On 10/4/2011 1:10 PM, nguillot wrote:
> I would like to specialize a templated function for the type
> std::vector<U>, U being a typename.
> I think it is not possible.
>
> However I read on forum we can (I describe just after how) but I guess
> this is not specialization but overload.
>
> I’ve got 2 questions:
> 1) Is it specialization or overload?
> 2) In fact, if it works, I don’t really care the answer of this
> question 1 (execpt for my knowledge): does the standard assure it
> works (resolution order) ?
>
> Now the stuff:
>
> Let’s have the template function:
>
> template<typename T>
> void f(const T& p_value)
> {
> cout<< "generic"<< endl;
> }
>
> The specialization (for bool for instance) is
>
> template<>
> void f<bool>(const bool& p_value)
> {
> cout<< "bool"<< endl;
> }
>
> The "specialization" for vector<U> is (or is it an overload?)
>
> template<typename U>
> void f(const std::vector<U> & p_value)
> {
> cout<< "vector"<< endl;
> }
>
> And indeed if I do :
>
> int i;
> vector<double> v;
> bool b;
>
> f(i);
> f(v);
> f(b);
>
> I get:
>
> generic
> vector
> bool
>
> So, is it really specialization?
> If no, am I sure f(v) will always calls this "specialization?


Here is how you can distinguish between an overload and a
specialization. An overload has the same name as the other function and
the specialization would have a different name, if the template
argument[s] is/are applied.

In your case the original function that you defined (with the const T&
argument) would have the name 'f<char>' if you substitute 'T' with
'char', right? Now, the one that has the vector as its argument, the
one with the template argument named 'U', would be named 'f<char>' if
you substitute 'U' with 'char', correct? So, the two names are the
same, but the functions are different, yes? So, it's an overload.

Here is another way of looking at it:
Since the 'vector' would otherwise be a partial specialization (since
it's a template in itself), and there are no partial specializations of
function templates, it can't be a specialization. It's an overload.

V
--
I do not respond to top-posted replies, please don't ask

Noah Roberts 10-04-2011 06:21 PM

Re: specialization or overload
 
On Oct 4, 10:10*am, nguillot <nicolas.guil...@gmail.com> wrote:
> Hello.
>
> I would like to specialize a templated function for the type
> std::vector<U>, U being a typename.
> I think it is not possible.
>
> However I read on forum we can (I describe just after how) but I guess
> this is not specialization but overload.
>
> I’ve got 2 questions:
> 1) * * *Is it specialization or overload?
> 2) * * *In fact, if it works, I don’t really care the answer of this
> question 1 (execpt for my knowledge): does the standard assure it
> works (resolution order) ?


It will work in name resolution if the overload is within current
scope or within scope of the type that it is an overload for.

The main difference between doing a specialization and an overload is
that if you're going to explicitly supply the template argument you
can't use an overload. Otherwise you SHOULD use the overload.
>
> Now the stuff:
>
> Let’s have the template function:
>
> template <typename T>
> void f(const T& p_value)
> {
> * * cout << "generic" << endl;
>
> }
>
> The specialization (for bool for instance) is
>
> template <>
> void f<bool>(const bool& p_value)
> {
> * * cout << "bool" << endl;
>
> }
>
> The "specialization" for vector<U> is (or is it an overload?)
>
> template <typename U>
> void f(const std::vector<U> & p_value)
> {
> * * cout << "vector" << endl;
>
> }
>
> And indeed if I do :
>
> * * int i;
> * * vector<double> v;
> * * bool b;
>
> * * f(i);
> * * f(v);
> * * f(b);
>
> I get:
>
> generic
> vector
> bool
>
> So, is it really specialization?
> If no, am I sure f(v) will always calls this "specialization?


It is an overload and with that syntax it will get called so long as
it is within the searchable name scope. Name resolution rules are
complicated, especially how and when ADT applies. You'll need to
familiarize yourself with them.

You probably noticed that functions cannot be partially specialized.
You can't do this:

template < typename U >
void f<vector<U>>(vector<U> const&) {}

If you need partial specialization for a function you have to use an
interim class:

template < typename T >
struct f_impl
{
static void call(T const&) {}
};

template < typename T >
void f(T const& t) { f_impl<T>::call(t); }

You can then partially specialize f_impl.

Again, this trick is needed only when you need to call the function
with explicitly specified template parameters. If you're using
deduction then you probably want to provide overloads instead of
specializations, partial or otherwise.



All times are GMT. The time now is 01:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57