<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' version='2.0'><channel><atom:id>tag:blogger.com,1999:blog-30671072</atom:id><lastBuildDate>Tue, 05 May 2009 06:36:16 +0000</lastBuildDate><title>//</title><description>comments on c++ and issues of interest to c++ programmers</description><link>http://slashslash.info/</link><managingEditor>noreply@blogger.com (Jon)</managingEditor><generator>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-30671072.post-115606007871839642</guid><pubDate>Sun, 20 Aug 2006 07:02:00 +0000</pubDate><atom:updated>2006-08-27T23:32:22.493-07:00</atom:updated><title>Douglas Gregor on Variadic Templates</title><description>Doug is a post-doctoral research fellow in the &lt;a href="http://www.osl.iu.edu/" target="authors"&gt;Open Systems Lab&lt;/a&gt;, part of the &lt;a href="http://www.cs.indiana.edu/" target="authors"&gt;Computer Science&lt;/a&gt; department at &lt;a href="http://www.iub.edu/" target="authors"&gt;Indiana University, Bloomington&lt;/a&gt;. His email is dgregor -at- cs.indiana.edu and his home page is &lt;a href="http://www.osl.iu.edu/%7Edgregor" target="authors"&gt;http://www.osl.iu.edu/~dgregor.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: You've just finished implementing Variadic Templates. What the heck are Variadic Templates?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; Variadic templates are templates that can take an arbitrary number of "extra" template arguments. Most templates take a fixed number of parameters. For example, an STL map has 4 parameters (Key, Data, Compare, Alloc) while an STL pair has two parameters (T and U). But what happens when you want to generalize a pair&lt;T, U&gt; into a tuple&lt;T1, T2, ..., TN&gt; (for any N), like in the &lt;a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html" target="authors"&gt;Boost Tuples library&lt;/a&gt;? You really need tuple to take any number of template arguments: variadic templates allow you to do so, by creating class and function templates that accept any number of arguments. Variadic templates also allow you to perform transformations on all of these arguments in one step, turning many non-trivial metaprograms into simple one-line statements.&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: C++ programmers are familiar with the concept with variadic function parameters from C. But we've been cautioned to avoid them. Why are Variadic Templates safer?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; C-style variadic functions are unsafe because all type information is lost when passing arguments through C's "...". That's why only built-in and Plain Old Data types work with with C-style variadic functions, and even with those we're left wide open to &lt;a href="http://en.wikipedia.org/wiki/Format_string_attack" target="authors"&gt;format string attacks&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Variadic templates are safer because, as templates, they retain complete type information at compile-time. With printf() implemented as a variadic template, we don't need the format string to tell us the type of each argument, because we have its static type when we instantiate the template. So instead of crashing when printf() is given a format string that doesn't match the actual arguments, we can just detect the error and throw an exception.&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: How are VTs helpful? What do they make possible, easier, or better?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; Variadic templates make it easier to write templates that work with any number of arguments. You can fake variadic templates in the language, but the result is usually a mess of duplicated code, artificial upper limits on the number of template arguments, and &lt;a href="http://boost-consulting.com/tmpbook/preprocessor.html" target="authors"&gt;preprocessor meta-programming&lt;/a&gt;. If you don't believe me, look at the implementation of Boost's &lt;a href="http://www.boost.org/doc/html/function.html" target="authors"&gt;Function&lt;/a&gt;, &lt;a href="http://www.boost.org/libs/bind/bind.html" target="authors"&gt;Bind&lt;/a&gt;, or &lt;a href="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html" target="authors"&gt;Tuple&lt;/a&gt; libraries: we can shrink the implementation of these libraries from tens of thousands of lines of redundant code to a few pages of relatively simple code. Perhaps best of all, we can bring the &lt;a href="http://horstmann.com/" target="authors"&gt;March of Progress&lt;/a&gt; full circle, providing the succinct (and popular!) syntax of printf and scanf for all of C++, providing the type safety that we expect from the language. Although we haven't explored it in detail yet, we've also found that variadic templates are very powerful tools for &lt;a href="http://en.wikipedia.org/wiki/Template_metaprogramming" target="authors"&gt;template metaprogramming&lt;/a&gt;.&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: Where can we learn more about how to use them?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; The best source of information on variadic templates is in the introduction and proposal on the variadic templates web page: &lt;a href="http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html" target="authors"&gt;http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;The introduction gives a short description of the implementation of a completely type-safe printf using variadic templates. The (much longer) proposal provides annotated implementations of several interesting facilities that make heavy use of variadic templates. Like all language features, the best way to understand a feature is to try it out: the examples in the proposal are a good starting point.&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: This sounds like a language extension that can't be implemented with a library. Is this a compiler mod? Which compiler? How can we get it to play with?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; Variadic templates cannot be implemented purely in a library, so I've implemented them in the GNU C++ compiler. Patches against the latest version of GCC are available from &lt;a href="http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html" target="authors"&gt;http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html&lt;/a&gt;. I encourage anyone interested in variadic templates to give the compiler a try: even after thinking about variadic templates for four years, it wasn't until I got to play with them that I really understood how they worked. Of course, I welcome any feedback!&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: So when will this be part of the official GCC release?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; I expect to officially submit the variadic templates patches to GCC in the very near future. After that, the decision of when to ship variadic templates is in the hands of the GCC maintainers. The good news is that variadic templates have been discussed before on the GCC mailing lists, and there is an &lt;a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20599" target="authors"&gt;open feature request&lt;/a&gt; from one of the maintainers for its inclusion. And unlike our &lt;a href="http://www.generic-programming.org/software/ConceptGCC/" target="authors"&gt;other GCC hack&lt;/a&gt;, the implementation of variadic templates is quite small and quite stable. Optimistically, I think we could see variadic template support in the official GCC 4.2.1 or 4.3.0.&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: You are on the C++ Standards committee. Does this mean that we'll see VTs become part of the C++ Standard?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; I hope so. I will bring the latest variadic templates proposal to the upcoming C++ committee meeting in Portland, but the C++ committee as a whole decides what will go into the next C++ standard. Having a complete implementation in GCC helps greatly (implementability is the first major hurdle for a proposal), but the biggest factor for acceptance is whether the community wants this feature in the language. In particular, variadic templates have always been viewed as a niche feature for expert library developers. Do you like variadic templates? Write your local C++ committee representative or show your support on &lt;a href="http://groups.google.com/group/comp.std.c++" target="authors"&gt;comp.std.c++&lt;/a&gt;!&lt;br /&gt;&lt;p style="font-weight: bold;"&gt;//: Who do you think &lt;a href="http://en.wikipedia.org/wiki/Shakespearean_authorship" target="shakespeare"&gt;authored the Shakespearean canon&lt;/a&gt;?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Gregor:&lt;/span&gt; Alas, I think the truth of the matter is that William Shakespeare was "merely" a brilliant man, and any authorship questions are unnecessary drama. In truth, I am more interested in the mathematical side of things, and the story of &lt;a href="http://en.wikipedia.org/wiki/Nicolas_Bourbaki" target="authors"&gt;Nicolar Bourbaki&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='http://res1.blogblog.com/tracker/30671072-115606007871839642?l=slashslash.info%2Findex.html'/&gt;&lt;/div&gt;</description><link>http://slashslash.info/2006/08/douglas-gregor-on-variadic-templates.html</link><author>noreply@blogger.com (Jon)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-30671072.post-115561655184197405</guid><pubDate>Tue, 15 Aug 2006 04:25:00 +0000</pubDate><atom:updated>2006-08-14T21:36:44.720-07:00</atom:updated><title>and or not</title><description>I was looking at some of my code the other day with a co-worker. He noticed that I wasn't using the &amp;&amp;amp; or || operators and rarely the ! operator. Instead I was using "and," "or," and "not."&lt;br /&gt;&lt;br /&gt;I pointed out that I find them much clearer and easier to understand. Since programming for clarity is one of my top goals, it is a no brainer for me to use English words in place of more cryptic symbols.&lt;br /&gt;&lt;br /&gt;He agreed and asked what header he had to include to use this symbols and was surpised when I told him that no header is needed. These are not &lt;span style="font-style: italic;"&gt;gasp!&lt;/span&gt; macros. All of these are reserved words and part of the core language for Standard C++.&lt;br /&gt;&lt;br /&gt;You can start using this words right now. And I encourage you to do so.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='http://res1.blogblog.com/tracker/30671072-115561655184197405?l=slashslash.info%2Findex.html'/&gt;&lt;/div&gt;</description><link>http://slashslash.info/2006/08/and-or-not.html</link><author>noreply@blogger.com (Jon)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-30671072.post-115544297655848672</guid><pubDate>Sun, 13 Aug 2006 04:07:00 +0000</pubDate><atom:updated>2006-08-27T23:33:18.726-07:00</atom:updated><title>Bjarne Stroustrup on Teaching C++</title><description>&lt;p style="font-weight: bold;"&gt;//: You left Bell Labs to accept a position at Texas A&amp;M working with &lt;span style="font-style: italic;"&gt;!eek!&lt;/span&gt; college students. How long ago did you do this and why?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; I left New Jersey for Texas more than three years ago.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Students aren't "&lt;span style="font-style: italic;"&gt;!eek!&lt;/span&gt;". Teaching was one of the reasons I went to academia. Had I not wanted to teach, I could have stayed at AT&amp;T Labs - my job there was full-time research. I felt that I - after 25 years of research and application - had something pretty unique to teach the students. Unfortunately, the average professor have never practiced what they preach and that sometimes weakens the message they send.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;It was time for a change. How many people today have stayed in one job for 25 years? It was the most varied of "jobs" and actually, I still do part of it because I didn't sever my connection with AT&amp;amp;T Labs completely (they didn't allow me to :-) and there are continuation in my projects. Academia is different from industrial research in many ways, some good, some not so good, but it is different, and "different" is stimulating.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.tamu.edu/" target="authors"&gt;Texas A&amp;M university&lt;/a&gt; is one of the few major US universities that is currently going through a period of major expansion - in quality and quantity. For example, our &lt;a href="http://www.cs.tamu.edu/" target="authors"&gt;CS department&lt;/a&gt; hired 4 new assistent professors this year.  You can see and feel the rapid improvement of department . It is great to be part of an improving and expanding organization! This year, I have papers in &lt;a href="http://www.cs.princeton.edu/%7Edpw/popl/06/" target="papers"&gt;POPL&lt;/a&gt; and &lt;a href="http://www.oopsla.org/2006/submission/research_papers/concepts:_first-class_language_support_for_generic_programming_in_c.html" target="authors"&gt;OOPSLA&lt;/a&gt; with colleagues - for good and bad, writing papers is a much more central part of academic life than the life of an industrial researcher.&lt;br /&gt;&lt;/p&gt;&lt;p style="font-weight: bold;"&gt;//: What level of students do you teach?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; I teach freshmen and graduates. Currently, I teach my graduate students practical language design through case studies from the C++0x effort. Before that, I was focussing on the software frameworks for distributed programming (CORBA, .Net, J2EE). I think that graduate students know a lot about the science of computer science, but too little about the theory and practice of constructing quality software. For example, they tend to know their algorithms, data structures, and machine architectures, but not how to combine the knowledge from those academic fields into maintainable and well-preforming software at a real-world scale.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;So, with my freshmen, I teach what I plan to be the basis for a more effective approach to designing and implementing software. Together with experienced colleagues, I have taught a first programming course for engineering freshmen, which from this fall will also become the standard first course for computer science freshmen.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;We are completely overhauling our undergraduate curriculum, and I expect the graduate curriculum will have to be revised to keep up.&lt;br /&gt;&lt;/p&gt;&lt;p style="font-weight: bold;"&gt;//: What have you learned from observing students as they learn C++?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; They are bright, but mistaught. The high schools have drilled them more than educated them and given them an unrealistic idea of what it takes to succeed in today's world. Most think that doing well is to regurgitate information and to be able to compute the answer to a question that you have seen in many slightly different versions before! Real search for answers and synthesis of systems from parts seems very novel to most. Most - being bright (most of our engineering students were near the top of their class) - also found it too easy to succeed in highschool. They therefore are surprised that in university it takes more work and smarter work just to be average!&lt;br /&gt;&lt;/p&gt;&lt;p&gt;None of this has much to do with C++, but the most important aspect of teaching freshment is to reajust their expectation of what is required. We would do them a severe disservice if we allowed them not to grow.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Fortunately - being bright and ambitious - many seem to be stimulated by the new challenges and approaches.&lt;br /&gt;&lt;/p&gt;&lt;p style="font-weight: bold;"&gt;//: Has this changed the way you feel about C++ or its future? What changes to the standard language or library might we see because of what you've learned while teaching?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; My view has not been fundamentally changed, but some views have been reinforced. I felt that C++ had become too "expert friendly" and unnecessily hard to teach. I hope to deal with some of that in C++0x, but it is a tough task because of course everyone voting in the standards committees is an expert of some sort, so attention naturally shifts towards what helps experts. I constantly try to yank their attention back to novices - "novices of all backgrounds" because as long as we keep learning we are all novices at something.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;For concrete examples, see &lt;a href="http://www.artima.com/cppsource/cpp0x.html" target="authors"&gt;my writings on C++0x&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p style="font-weight: bold;"&gt;//: For awhile, Java was all the rage in college curriculums. Has that changed or are college grads with C++ knowledge going to be getting rare?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; I don't have the global view to answer that question with any certainty, but it seems that most students have some knowledge of C++. C++ still dominates the "harder" areas where performance (time or space) is important or where the distance between Java and the machine is a problem. I have seen a few school switch to C++ recently and a lot of embedded systems projects (mostly in industry) move to C++. The apparent shortage of C++ programmers is partly an increased demand and a mismatch between what is taught (fairly mondane PC projects) and where the increase in C++ usage occur (demanding, large, high-performance, high-reliabily applications).&lt;br /&gt;&lt;/p&gt;&lt;p&gt;For a short list of C++ applications, see &lt;a href="http://www.research.att.com/%7Ebs/applications.html" target="authors"&gt;the list on my home page&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Interestingly, we (I personally and TAMU CS) have had quiet a few requests from industry (big name companies) to increase our emphasis on software development skills and skills that prepare the students to work closer to the hardware. That, of course means C++ and I'm keen to make sure that it means modern C++ rather than glorified assembly code expressed in C style.&lt;br /&gt;&lt;/p&gt;&lt;p style="font-weight: bold;"&gt;//: There are a number of good C++ books for professionals (including several that have been written or edited by you), but the story is different for C++ text books. What is your opinion of the C++ text books currently on offer? What do you use in your classes? Might we see a text book by Bjarne Stroustrup in the future?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; I have not been impressed by the current crop of C++ books for beginners (the books by Francis Glassborow and Koenig&amp;Moo are exceptions, but address niche markets), so yes, I had to construct the materials for my beginning programming course from scratch. Of course I use C++. This is likely to become available as a book next year after being field tested on a few hundred freshmen for a couple of years.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I call my approach "depth first". The aim is to get to writing realistc code as soon as possible (e.g. file processing is week 6 and GUI is week 8) together with an emphasis on correctness and maintainability.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;We teach the basic use of the STL together with the basics of its implementation and general principles.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Fundamentals are important in this time of rapid change because fundamentals is what changes the least.&lt;br /&gt;&lt;/p&gt;&lt;p style="font-weight: bold;"&gt;//: Who do you think &lt;a href="http://en.wikipedia.org/wiki/Shakespearean_authorship" target="shakespeare"&gt;authored the Shakespearean canon&lt;/a&gt;?&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Stroustrup:&lt;/span&gt; No opinions. I'm not really a Shakespeare buff. I like history better; try &lt;a href="http://www.pepysdiary.com/" targe="pepys"&gt;the world's oldest blog&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='http://res1.blogblog.com/tracker/30671072-115544297655848672?l=slashslash.info%2Findex.html'/&gt;&lt;/div&gt;</description><link>http://slashslash.info/2006/08/bjarne-stroustrup-on-teaching-c.html</link><author>noreply@blogger.com (Jon)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>3</thr:total></item></channel></rss>