File: nocodebloat.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 (50 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download | duplicates (4)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    As mentioned in section ref(TEMPFUNDECL), the linker removes
 hi(linker: removing identical template instantiations) identical
instantiations of a template from the final program, leaving only one
instantiation for each unique set of actual template type parameters. To
illustrate the linker's behavior we do as follows:
    itemization(
    it() First we construct several source files:
            itemization(
            itt(source1.cc) defines a function tt(fun), instantiating
tt(add) for tt(int)-type arguments, including tt(add)'s template
definition. It displays tt(add)'s address using tt(union PointerUnion):
        verbinclude(-a examples/pointerunion.h)
    
        Here is a program using tt(PointerUnion):
        verbinclude(-a examples/source1.cc)
            itt(source2.cc) defines the same function, but merely declares the
proper tt(add) template using a template declaration (em(not) an instantiation
declaration). Here is tt(source2.cc):
        verbinclude(-a examples/source2.cc)
            itt(main.cc) again includes tt(add)'s template definition,
declares the function tt(fun) and defines tt(main), defining tt(add)
for tt(int)-type arguments as well and displaying tt(add)'s function
address. It also calls the function tt(fun). Here is tt(main.cc):
        verbinclude(-a examples/main.cc)
            )
    it() All sources are compiled to object modules. Note the different sizes
of tt(source1.o) (1912 bytes using tt(g++) version 4.3.4 (sizes of object
modules reported in this section may differ for different compilers and/or
run-time libraries)) and tt(source2.o) (1740 bytes). Since tt(source1.o)
contains the instantiation of tt(add), it is somewhat larger than
tt(source2.o), containing only the template's declaration. Now we're ready to
start our little experiment.
    it() Linking tt(main.o) and tt(source1.o), we obviously link together two
object modules, each containing its own instantiation of the same template
function. The resulting program produces the following output:
        verb(    0x80486d8
    0x80486d8)

Furthermore, the size of the resulting program is 6352 bytes.
    it() Linking tt(main.o) and tt(source2.o), we now link together an object
module containing the instantiation of the tt(add) template, and another
object module containing the mere declaration of the same template
function. So, the resulting program cannot but contain a single instantiation
of the required function template. This program has exactly the same size, and
produces exactly the same output as the first program.
    )
    From our little experiment we conclude that the linker indeed removes
identical template instantiations from a final program. Furthermore we
conclude that using mere template declarations does not result in template
instantiations.