![]() |
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. |
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 |
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.