File: explicit.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (24 lines) | stat: -rw-r--r-- 1,490 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    In the previous section we saw that the compiler may encounter ambiguities
when attempting to instantiate a template. In an example overloaded versions
of a function (tt(fun)) existed, expecting different types of arguments. The
ambiguity resulted from the fact that both arguments could have been provided
by an instantiation of a function template. The intuitive way to solve such an
ambiguity is to use a tt(static_cast). But casts should be avoided wherever
possible.

    With function templates static casts may indeed be avoided using
 emi(explicit template type arguments). Explicit template type arguments can
be used to inform the compiler about the actual types it should use when
instantiating a template. To use explicit type arguments the function's name
is followed by an em(actual template type argument list) which may again be
followed by the function's argument list. The actual types mentioned in the
actual template argument list are used by the compiler to `deduce' what types
to use when instantiating the template. Here is the example from the previous
section, now using explicit template type arguments:
        verbinclude(-a examples/explicit.cc)

    Explicit template type arguments  can be used in situations where the
compiler has no way to detect which types should actually be used. E.g., in
section ref(TEMPFUNARGS) the function template tt(Type fun()) was defined. To
instantiate this function for the tt(double) type, we can call
tt(fun<double>()).