Global Sources
EE Times-Asia
Stay in touch with EE Times Asia
?
EE Times-Asia > Embedded
?
?
Embedded??

Templates: Implementing template generators

Posted: 20 Apr 2016 ?? ?Print Version ?Bookmark and Share

Keywords:C? API? compiler? programming language? interface?

C++ is a divisive force. For every discussion about it, a certain percentage of people will love it and another substantial group will hate it. Above all, one feature highlights this divide: templates. Templates have many problems: they slow compile times, force implementations to be specified in headers and can bloat generated code. However they're also a powerful mechanism for programming against generic types.

In the first article in this series, we saw how C++ templates have significant limitations, most notably that they can't be used across API boundaries. We also investigated a few implementations for run-time templates, but found they either didn't work or had unacceptable costs. In this part I'll explain how run-time templates can be implemented in an efficient way using a new technique called 'Template Generators'.

Template Generators
It turns out the stack isn't workable, because the stack frames can disappear before we've actually used the interface reference to call a method. We can't use the heap, since it requires management overhead and is largely opaque to optimisations.

Fortunately we can use generated code to solve this problem. Template generators are functions emitted by the compiler which can be called and will return template arguments. Sounds simple, right?

OK, let's look at this example again:

Clearly, no one function knows all of the arguments to h(). g() knows the second argument will be, but has no idea about the first argument. f() knows what it's giving to g(), but doesn't know anything about h().

However we can see there's a clear stack pattern here. The top of the stack is f(), which passes its template arguments down into g(), etc. As we saw previously though, this 'template' stack outlives the run-time stack. Conveniently, however, this stack is unchanging.

So let's write functions to represent this stack:

If you call root_template_generator() with a large enough array it'll fill the array with the template arguments of h(). This is a start, but we need a way to get the template arguments of g() as well.

Paths
The issue we've hit here is that we have a tree with a root template generator, an intermediate template generator and a leaf template generator and we want to be able to specify a path through that tree that supports early exit. Fortunately we can represent the path extremely compactly via a binary string.

For example:
???To get arguments of g(): '1'
???To get arguments of h(): '10'

Firstly, we start every path with a '1' so that we can compute the beginning of the path efficiently when it's represented as a integer. Then at each node in the template generator tree there are a number of possible exit transitions. One of these transitions is to return immediately with the current template arguments. Other transitions would be to continue down the tree.

1???2???3?Next Page?Last Page



Article Comments - Templates: Implementing template gen...
Comments:??
*? You can enter [0] more charecters.
*Verify code:
?
?
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

?
?
Back to Top