Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Partial template specialization
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Caveats=== C++ templates are not limited to classes - they can also be used to define [[Template (C++)#Function templates|function templates]]. Although function templates can be fully specialized, they ''cannot'' be partially specialized, irrespective of whether they are member function templates or non-member function templates. This can be beneficial to compiler writers, but affects the flexibility and granularity of what developers can do.<ref name=Alexandrescu2001Modern>{{cite book|last1=Alexandrescu|first1=Andrei|authorlink1=Andrei Alexandrescu|title=[[Modern C++ Design]]|date=1 February 2001|publisher=Addison Wesley|isbn=0-201-70431-5|page=23}}</ref> But, function templates can be [[Function overloading|overloaded]], which gives nearly the same effect as what partial function template specialization would have.<ref>{{cite journal|last1=Sutter|first1=Herb|authorlink1=Herb Sutter|title=Why Not Specialize Function Templates?|journal=C/C++ Users Journal|date=July 2001|volume=19|issue=7|url=http://www.gotw.ca/publications/mill17.htm|accessdate=7 December 2014}}</ref> The following examples are provided to illustrate these points. <syntaxhighlight lang="cpp"> // legal: base function template template <typename ReturnType, typename ArgumentType> ReturnType Foo(ArgumentType arg); // legal: explicit/full function template specialization template <> std::string Foo<std::string, char>(char arg) { return "Full"; } // illegal: partial function template specialization of the return type // function template partial specialization is not allowed // template <typename ArgumentType> // void Foo<void, ArgumentType>(ArgumentType arg); // legal: overloads the base template for a pointer argument type template <typename ReturnType, typename ArgumentType> ReturnType Foo(ArgumentType *argPtr) { return "PtrOverload"; } // legal: base function name reused. Not considered an overload. ill-formed: non-overloadable declaration (see below) template <typename ArgumentType> std::string Foo(ArgumentType arg) { return "Return1"; } // legal: base function name reused. Not considered an overload. ill-formed: non-overloadable declaration (see below) template <typename ReturnType> ReturnType Foo(char arg) { return "Return2"; } </syntaxhighlight> In the example listed above, note that while the last two definitions of the function <code>Foo</code> are legal C++, they are considered ill-formed according to the standard because they are non-overloadable declarations.<ref>{{cite web | url=https://isocpp.org/files/papers/N3690.pdf | title=ISO/IEC JTC1 SC22 WG21 N 3690: Programming Languages β C++ | publisher=ISO | date=15 May 2013 | accessdate=16 October 2016 | pages=294 | quote={{em | 13.1 Overloadable declarations [over.load] Not all function declarations can be overloaded. Those that cannot be overloaded are specified here. A program is ill-formed if it contains two such non-overloadable declarations in the same scope.}}}}</ref> This is because the definition of function overloading only accounts for the function name, parameter type list and the enclosing namespace (if any). It does not account for the return type.<ref>{{cite web | url=https://isocpp.org/files/papers/N3690.pdf | title=ISO/IEC JTC1 SC22 WG21 N 3690: Programming Languages β C++ | publisher=ISO | date=15 May 2013 | accessdate=16 October 2016 | pages=294β296 | quote={{em | 13.1 Overloadable declarations [over.load]}}}}</ref> However, these functions can still be called by explicitly indicating the signature to the compiler, as demonstrated by the following program. <syntaxhighlight lang=cpp> // note: to be compiled in conjunction with the definitions of Foo above int main(int argc, char *argv[]) { char c = 'c'; std::string r0, r1, r2, r3; // let the compiler resolve the call r0 = Foo(c); // explicitly specify which function to call r1 = Foo<std::string>(c); r2 = Foo<std::string, char>(c); r3 = Foo<std::string, char>(&c); // generate output std::cout << r0 << " " << r1 << " " << r2 << " " << r3 << std::endl; return 0; } //expected output: Return1 Return2 Full PtrOverload </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)