~ubuntu-branches/ubuntu/gutsy/c++-annotations/gutsy

« back to all changes in this revision

Viewing changes to yo/templatefunctions/explicit.yo

  • Committer: Bazaar Package Importer
  • Author(s): Frank B. Brokken
  • Date: 2007-06-15 19:31:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070615193112-i48grmnykrf1ipqf
Tags: 7.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
    In the previous section (section ref(TEMPFUNINST)) we've seen that the
2
2
compiler may encounter ambiguities when attempting to instantiate a
3
3
template. We've seen an example in which overloaded versions of a function
4
 
tt(fun()) existed, expecting different types of arguments, both of which
5
 
could have been provided by an instantiation of a template function. The
6
 
intuitive way to solve such an ambiguity is to use a tt(static_cast) type
7
 
cast, but as noted: if possible, casts should be avoided.
 
4
tt(fun()) existed, expecting different types of arguments, both of which could
 
5
have been provided by an instantiation of a function template. The intuitive
 
6
way to solve such an ambiguity is to use a tt(static_cast), but as noted: if
 
7
possible, casts should be avoided.
8
8
 
9
 
    When template functions are involved, such a tt(static_cast) may indeed
 
9
    When function templates are involved, such a tt(static_cast) may indeed
10
10
neatly be avoided, using emi(explicit template type arguments). When
11
11
explicit template type arguments are used the compiler is explicitly
12
12
informed about the actual template type parameters it should use when
20
20
same example as given in the previous section, now using  explicit template
21
21
type arguments:
22
22
        verbinclude(templatefunctions/examples/explicit.cc)
 
23
 
 
24
    Explicit template argument types can be used in situations where the
 
25
compiler has no way to detect which types should actually be used. E.g., in
 
26
section ref(TEMPFUNARGS) the function template tt(Type fun()) was defined. To
 
27
instantiate this function for the tt(double) type, we can use
 
28
tt(fun<double>()).