Prev: pros and cons of returning const ref to string instead of string by value
Next: Using preprocessor macro as a template parameter
From: piwi on 3 Dec 2009 23:13 Hello, I have an issue when performing partial specializing of a template function. Please consider the following sample: <code> template <typename First, typename Second> foo(First const& one, Second const& two) { // ... } template <typename Second> foo<STRING, Second>(STRING const& one, Second const& two) { // ... } int main() { STRING s; foo (s, 10); return 0; } </code> I want to call my routine without specifying my templates; my code is obviously somewhat more complex than above: some template arguments are intended to be function signatures. { according to the poster's request to moderators, the next paragraph should start with "VC 2005" and not "VC 2008". -mod } VC 2008 raises a C2768 error code (illegal use of explicit template arguments). What I understand is that the compiler is lost because it does not know whether it is a template specialization or a new definition. Any ideas? Thanks! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Balog Pal on 5 Dec 2009 07:06 "piwi" <bruno.lemarchand(a)gmail.com> > Hello, I have an issue when performing partial specializing of a > template function. You can't do that. Partial specialisation is for class templates only. Function templates can be fully specialized or overloaded, the last often usable to similar effect as partial spec, but may lead to surprizes or ambiguities. Please consider the following sample: > > <code> > > template <typename First, typename Second> > foo(First const& one, Second const& two) > { > // ... > } > > template <typename Second> > foo<STRING, Second>(STRING const& one, Second const& two) > { > // ... > } That would be partial spec that is wrong. You may try template <typename Second> foo(STRING const& one, Second const& two) { // ... } that is an overload. And can be selected as a better match for many situations, but be prepared that even slight conversions needed on the first argument, that are routinely done on simple function calls may make the original template selected. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nick Hounsome on 6 Dec 2009 03:17 On Dec 6, 12:06 am, "Balog Pal" <p...(a)lib.hu> wrote: > "piwi" <bruno.lemarch...(a)gmail.com> > > > Hello, I have an issue when performing partial specializing of a > > template function. > > You can't do that. Partial specialisation is for class templates only. > Function templates can be fully specialized or overloaded, the last often > usable to similar effect as partial spec, but may lead to surprizes or > ambiguities. > > Please consider the following sample: > > > > > <code> > > > template <typename First, typename Second> > > foo(First const& one, Second const& two) > > { > > // ... > > } > > > template <typename Second> > > foo<STRING, Second>(STRING const& one, Second const& two) > > { > > // ... > > } > > That would be partial spec that is wrong. You may try > > template <typename Second> > foo(STRING const& one, Second const& two) > { > // ... > > } > > that is an overload. And can be selected as a better match for many > situations, but be prepared that even slight conversions needed on the first > argument, that are routinely done on simple function calls may make the > original template selected. The other way to go is to have a single template function call a static function of a template class that CAN be partially specialized. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jesse Perla on 6 Dec 2009 11:04 On Dec 5, 7:06 pm, "Balog Pal" <p...(a)lib.hu> wrote: > "piwi" <bruno.lemarch...(a)gmail.com> > > > Hello, I have an issue when performing partial specializing of a > > template function. > > You can't do that. Partial specialisation is for class templates only. I don't know what you are trying to do with the function partial- specialization, but frequently it is because you want different overloads with generic types. To solve this problem, I use boost::enable_if (http://www.boost.org/doc/libs/1_41_0/libs/utility/ enable_if.html) all the time and find the small amounts of metaprogramming easy enough. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: piwi on 7 Dec 2009 02:53
On Dec 7, 5:04 am, Jesse Perla <jessepe...(a)gmail.com> wrote: > On Dec 5, 7:06 pm, "Balog Pal" <p...(a)lib.hu> wrote: > > > "piwi" <bruno.lemarch...(a)gmail.com> > > > > Hello, I have an issue when performing partial specializing of a > > > template function. > > > You can't do that. Partial specialisation is for class templates only. > > I don't know what you are trying to do with the function partial- > specialization, but frequently it is because you want different > overloads with generic types. To solve this problem, I use > boost::enable_if (http://www.boost.org/doc/libs/1_41_0/libs/utility/ > enable_if.html) all the time and find the small amounts of > metaprogramming easy enough. > > -- > [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ] > [ comp.lang.c++.moderated. First time posters: Do this! ] Thanks all. For some reason I never thought that partial specialization was not possible for template functions! But considering it now, somehow, it makes some sense. Indeed, overloading could mimic the behavior I'm looking for, but that's true it may lead to ambiguities. About using boost, because my code is integrated into a large software framework, I'm unfortunately not allowed to use it, so enable_if is not an alternative in my situation. However, changing this function into a static method of a templated class may do the deal, I'll try that. How come is partial specialization not allowed for template functions? Is it because, precisely, it would get in the way of the overloading mechanism? I'm guessing that a corollary is that even partial specialization is not possible for templated methods? I guess all that is mentionned in Stroustrop's The C++ Langage Programming, but I'm guilty of not having it next to me right now -- I'm not at work today ;-) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |