File: specializations.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (32 lines) | stat: -rw-r--r-- 1,808 bytes parent folder | download | duplicates (3)
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
Class templates can be (partially) specialized. Specializations are commonly
used to fine-tune implementations for specific types. Concepts can also be
used when specializations are defined. Consider a tt(struct Handler) having
the following generic implementation:
    verbinsert(-s4 //generic examples/specializations.cc)

    In addition to possibly type-related specializations (like a tt(struct
Handler<Tp *> ...)) a specialization requiring the availability of the
addition operator on tt(Tp) can be defined by requiring the concept
tt(Addable):
    verbinsert(-s4 //addable examples/specializations.cc)

    When used in the following program (assuming all required headers were
included), the first line of the output shows em(Generic Handler), while
the second line shows em(Handler for types supporting operator+):
    verbinsert(-s4 //use examples/specializations.cc)
    
    The compiler, compiling tt(main's) first statement, first looks for
a specialized version of tt(Handler). Although it finds one, that
specialization requires the availability of tt(operator+). As that operator is
not available for tt(std::vector) the compiler does not use that
specialization. Had this been the only available implementation, then the
compiler would have reported a tt(constraints not satisfied) error. However,
there's still the generic definition which em(can) be used for
tt(std::vector). Therefore the compiler uses the generic definition (which is
at the same time provides a nice illustration of the SFINAE (cf. section
ref(SFINAE)) principle). 

When instantiating the second tt(Handler) object the addition operator em(is)
available, and so in that case the compiler selects the specialized version:
where available, specializations are used; if not, then generic template
definitions are used.