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!
===Example=== Suppose there exists a <code>KeyValuePair</code> class with two template parameters, as follows. <syntaxhighlight lang="cpp"> template <typename Key, typename Value> class KeyValuePair {}; </syntaxhighlight> The following is an example of a class that defines an explicit full template specialization of <code>KeyValuePair</code> by pairing integers with strings. The class type retains the same name as the original version. <syntaxhighlight lang="cpp"> template <> class KeyValuePair<int, std::string> {}; </syntaxhighlight> The next is an example of partial specialization of <code>KeyValuePair</code> with the same name as the original version and one specialized template parameter. <syntaxhighlight lang="cpp"> template <typename Key> class KeyValuePair<Key, std::string> {}; </syntaxhighlight> The next example class <code>KeyStringPair</code> is [[Inheritance (object-oriented programming)|derived]] from the original <code>KeyValuePair</code> with a new name, and defines a partial template specialization. In contrast to the explicit specialization above, only the ''Value'' template parameter of the [[Inheritance (object-oriented programming)#Subclasses and superclasses|superclass]] is specialized, while the ''Key'' template parameter remains generic. <syntaxhighlight lang="cpp"> template <typename Key> class KeyStringPair : public KeyValuePair<Key, std::string> {}; </syntaxhighlight> It does not matter which template parameters are specialized and which remain generic. For instance, the following is also a valid example of a partial specialization of the original <code>KeyValuePair</code> class. <syntaxhighlight lang="cpp"> template <typename Value> class IntegerValuePair : public KeyValuePair<int, Value> {}; </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)