Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Templates

Advanced CPP
by

Lazlo Alexandru

on 11 July 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Templates

The result type of the expression “A + B” is:

Glue
<
Item
,

Item
>

Doing “A + B + C” we're in effect doing

Glue
<
Item
,
Item
> +
Item

which results in a temporary Glue instance of type:

Glue
<
Glue
<
Item, Item
>,
Item
>
Template meta programming
Item class
Output
Simple Solution
Using Glue
Output
Item class
Glue class
Analysis
Problems:
extra
memory
to store the temporary
extra
time
to create temporary and to copy temporary to X
X = A + B
Y
Creates a temporary Item
Y
Copies the temporary item
Y
to X via the copy operator
More problems ...
X = A + B + C
A+B
creates a temporary
TMP1
TMP1
+C creates a temporary
TMP2
TMP2 is copied into X through the copy operator
we use more memory and time than needed ...
Solution ?
Accepting Glue
Eliminating temporary
X = A + B
Glue class holds reference to A and B
X = Glue(A, B)
Going further
Output
Super-Glue
General approach
we need a way to overload the
+ operator
for all possible combinations of
Glue
and
Item

the + operator needs to accept arbitrarily long
Glue
types, eg:

Glue
<
Glue
<
Glue
<
Item
,
Item
>,
Item
>,
Item
>

we also need the
Item
class to accept arbitrarily long
Glue
types
Strange base class
function
Base
<
T
>::
getRefToDerived
() will give us a reference to
T
(static polymorphism)
another way of thinking:
Base
<
T
> is a wrapper for class
T
, where class
T
can be anything
Intro
Compile time class generation
Code duplication
Advanced C++
Item class
Let's derive the
Item
class from the
Base
class:
an

Item
object can be interpreted as a
Base<
Item
>
object
function
Base<
Item
>
::
getRefToDerived
() will give us a reference to our
Item
object
Glue class
Let's derive the
Glue
class from the
Base
class:
a
Glue
<
T1
,
T2
> object can be interpreted as a
Base
<
Glue
<
T1
,
T2
> > object
function
Base
<
Glue
<
T1
,
T2
> >::
getRefToDerived()
will give us a reference to our
Glue
<
T1
,
T2
> object
The + operator
We can now define a simple looking + operator:
both the
Glue
and
Item
classes are derived from the
Base
, so operator+() accepts both
Glue
and
Item
Example 1
operator+() can now handle arbitrarily long expressions, eg:
X = A + B + C + D + E + F + G + H ;
Item
A;
Item
B;
Item
X = A + B;
the
A
can be interpreted as both a
Item
and a
Base
, hence operator+() sees
A
as having the type
Base
<
Item
>
taking template expansion into account, we're in effect calling operator+() as follows:
const Glue<
Item
,
Item
>
operator+(const
Base
<
Item
>& A, const
Base
<
Item
>& B)
{
return Glue<
Item
,
Item
>( A.getRefToDerived(), B.getRefToderived() );
}
inside operator+(), calling
A.getRefTo Derived()
gives reference to the derived type of
Base
<
Item
>, which is
Item
Example 2
Item
A;
Item
B;
Item
C;
Item
X = A + B + C;
for the first
+
, we're in effect calling operator+() as:
operator+(const Base<
Item
>& A, const Base<
Item
>& B)
produces a temporary of type
Glue
<
Item
,
Item
>
for the second +, we're in effect calling operator+() as:
operator+(const Base< Glue<
Item
,
Item
> >& A, const Base<
Item
>& B)
produces a temporary of type
Glue
<
Glue
<
Item
,
Item
>,
Item
>
Accepting Glue
Let's modify the
Item
class to accept arbitrarily long
Glue
types
Glue
<
Glue
<
Glue
<
Item
,
Item
>,
Item
>,
Item
>
to do this, we first need a way of getting:
(a) the number of
Item
instances in a
Glue
type
(b) the address of each
Item
in a
Glue
instance
Depth of structure
Glue
<
Glue
<
Item
,
Item
>,
Item
>
T1
T2
Acquire address
Glue
<
Glue
<
Item
,
Item
>,
Item
>
T2
T1
Modified Item
Finish
http://arma.sourceforge.net
Source:
Questions ?
X = Glue(A, B)
X = A+B
1. Iteration
2. Iteration
1. Iteration
2. Iteration
Summary
compiling
heavy template code takes longer than non template code
execution speed
can be very fast
!
not all C++ compilers can propperly handle heavy template meta-programming
Recomanded compilers:
GCC (Linux, Windows, Mac OS X)
Intel C++ compiler
Not recomanded:
Borland C++ builder has problems
MS Visual C++ prior to 2008
Solving a Problem
The Problem
Static Polimorphism
Base<
Derived
>*
p
;
Base
Derived
::getRefToDerived();
void main()
{
Item i1(10);
Item i2(20);
Item i3(30);
Item i4(40);

Item sum = i1 + i2 + i3 + i4;

cout << "Sum is: " << sum;
}
Item
... but what is with A + B +C + D
Item
Glue
Full transcript