source: trunk/doc/html/templates.html@ 208

Last change on this file since 208 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 9.9 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/object.doc:610 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Why doesn't Qt use templates for signals and slots?</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Why doesn't Qt use templates for signals and slots?</h1>
33
34
35
36<p> A simple answer is that when Qt was designed, it was not possible to
37fully exploit the template mechanism in multi-platform applications due
38to the inadequacies of various compilers. Even today, many widely used
39C++ compilers have problems with advanced templates. For example, you
40cannot safely rely on partial template instantiation, which is essential
41for some non-trivial problem domains. Thus Qt's usage of templates has
42to be rather conservative. Keep in mind that Qt is a multi-platform
43toolkit, and progress on the Linux/g++ platform does not necessarily
44improve the situation elsewhere.
45<p> Eventually those compilers with weak template implementations will
46improve. But even if all our users had access to a fully standards
47compliant modern C++ compiler with excellent template support, we would
48not abandon the string-based approach used by our <a href="metaobjects.html#meta-object">meta object</a> compiler.
49Here are five reasons why:
50<p> <h3>1. Syntax matters</h3>
51<p> Syntax isn't just sugar: the syntax we use to express our algorithms can
52significantly affect the readability and maintainability of our code.
53The syntax used for Qt's signals and slots has proved very successful in
54practice. The syntax is intuitive, simple to use and easy to read.
55People learning Qt find the syntax helps them understand and utilize the
56signals and slots concept -- despite its highly abstract and generic
57nature. Furthermore, declaring signals in class definitions ensures that
58the signals are protected in the sense of protected C++ member
59functions. This helps programmers get their design right from the very
60beginning, without even having to think about design patterns.
61<p> <h3>2. Precompilers are good</h3>
62<p> Qt's <tt><a href="moc.html#moc">moc</a></tt> (Meta Object Compiler) provides a clean way to go
63beyond the compiled language's facilities. It does so by generating
64additional C++ code which can be compiled by any standard C++ compiler.
65The <tt>moc</tt> reads C++ source files. If it finds one or more class
66declarations that contain the "Q_OBJECT" macro, it produces another C++
67source file which contains the meta object code for those classes. The
68C++ source file generated by the <tt>moc</tt> must be compiled and
69linked with the implementation of the class (or it can be
70<tt>#included</tt> into the class's source file). Typically <tt>moc</tt>
71is not called manually, but automatically by the build system, so it
72requires no additional effort by the programmer.
73<p> There are other precompilers, for example, <tt>rpc</tt> and
74<tt>idl</tt>, that enable programs or objects to communicate over
75process or machine boundaries. The alternatives to precompilers are
76hacked compilers, proprietary languages or graphical programming tools
77with dialogs or wizards that generate obscure code. Rather than locking
78our customers into a proprietary C++ compiler or into a particular
79Integrated Development Environment, we enable them to use whatever tools
80they prefer. Instead of forcing programmers to add generated code into
81source repositories, we encourage them to add our tools to their build
82system: cleaner, safer and more in the spirit of UNIX.
83<p> <h3>3. Flexibility is king</h3>
84<p> C++ is a standarized, powerful and elaborate general-purpose language.
85It's the only language that is exploited on such a wide range of
86software projects, spanning every kind of application from entire
87operating systems, database servers and high end graphics
88applications to common desktop applications. One of the keys to C++'s
89success is its scalable language design that focuses on maximum
90performance and minimal memory consumption whilst still maintaining
91ANSI-C compatibility.
92<p> For all these advantages, there are some downsides. For C++, the static
93object model is a clear disadvantage over the dynamic messaging approach
94of Objective C when it comes to component-based graphical user interface
95programming. What's good for a high end database server or an operating
96system isn't necessarily the right design choice for a GUI frontend.
97With <tt>moc</tt>, we have turned this disadvantage into an advantage,
98and added the flexibility required to meet the challenge of safe and
99efficient graphical user interface programming.
100<p> Our approach goes far beyond anything you can do with templates. For
101example, we can have object properties. And we can have overloaded
102signals and slots, which feels natural when programming in a language
103where overloads are a key concept. Our signals add zero bytes to the
104size of a class instance, which means we can add new signals without
105breaking binary compatibility. Because we do not rely on excessive
106inlining as done with templates, we can keep the code size smaller.
107Adding new connections just expands to a simple function call rather
108than a complex template function.
109<p> Another benefit is that we can explore an object's signals and slots at
110runtime. We can establish connections using type-safe call-by-name,
111without having to know the exact types of the objects we are connecting.
112This is impossible with a template based solution. This kind of runtime
113introspection opens up new possibilities, for example GUIs that are
114generated and connected from Qt Designer's XML <tt>ui</tt> files.
115<p> <h3>4. Calling performance is not everything</h3>
116<p> Qt's signals and slots implementation is not as fast as a template-based
117solution. While emitting a signal is approximately the cost of four
118ordinary function calls with common template implementations, Qt
119requires effort comparable to about ten function calls. This is not
120surprising since the Qt mechanism includes a generic marshaller,
121introspection and ultimately scriptability. It does not rely on
122excessive inlining and code expansion and it provides unmatched runtime
123safety. Qt's iterators are safe while those of faster template-based
124systems are not. Even during the process of emitting a signal to several
125receivers, those receivers can be deleted safely without your program
126crashing. Without this safety, your application would eventually crash
127with a difficult to debug free'd memory read or write error.
128<p> Nonetheless, couldn't a template-based solution improve the performance
129of an application using signals and slots? While it is true that Qt adds
130a small overhead to the cost of calling a slot through a signal, the
131cost of the call is only a small proportion of the entire cost of a
132slot. Benchmarking against Qt's signals and slots system is typically
133done with empty slots. As soon as you do anything useful in your slots,
134for example a few simple string operations, the calling overhead becomes
135negligible. Qt's system is so optimized that anything that requires
136operator new or delete (for example, string operations or
137inserting/removing something from a template container) is significantly
138more expensive than emitting a signal.
139<p> Aside: If you have a signals and slots connection in a tight inner loop
140of a performance critical task and you identify this connection as the
141bottleneck, think about using the standard listener-interface pattern
142rather than signals and slots. In cases where this occurs, you probably
143only require a 1:1 connection anyway. For example, if you have an object
144that downloads data from the network, it's a perfectly sensible design
145to use a signal to indicate that the requested data arrived. But if you
146need to send out every single byte one by one to a consumer, use a
147listener interface rather than signals and slots.
148<p> <h3>5. No limits</h3>
149<p> Because we had the <tt>moc</tt> for signals and slots, we could add
150other useful things to it that could not not be done with templates.
151Among these are scoped translations via a generated <tt>tr()</tt>
152function, and an advanced property system with introspection and
153extended runtime type information. The property system alone is a great
154advantage: a powerful and generic user interface design tool like Qt
155Designer would be a lot harder to write - if not impossible - without a
156powerful and introspective property system.
157<p> C++ with the <tt>moc</tt> preprocessor essentially gives us the
158flexibility of Objective-C or of a Java Runtime Environment, while
159maintaining C++'s unique performance and scalability advantages. It is
160what makes Qt the flexible and comfortable tool we have today.
161<p>
162<!-- eof -->
163<p><address><hr><div align=center>
164<table width=100% cellspacing=0 border=0><tr>
165<td>Copyright &copy; 2007
166<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
167<td align=right><div align=right>Qt 3.3.8</div>
168</table></div></address></body>
169</html>
Note: See TracBrowser for help on using the repository browser.