TITLE: NOTES FROM ANSI/ISO - TEMPLATE DEFAULT ARGUMENTS [ This month Glen McCluskey's C++ Newsletter has an interesting article on template default arguments. For brevity, I have removed the bodies of the other articles. See contact info at the bottom for obtaining the full text. -adc ] Issue #024 June, 1997 Contents: Pure Virtual Functions Introduction to Object-oriented Design Part 3 - Polymorphism Notes From ANSI/ISO - Template Default Arguments Introduction to STL Part 11 - Replacing PURE VIRTUAL FUNCTIONS [snip] INTRODUCTION TO OBJECT-ORIENTED DESIGN PART 3 - POLYMORPHISM [snip] NOTES FROM ANSI/ISO - TEMPLATE DEFAULT ARGUMENTS Jonathan Schilling, jls@sco.com If you look at the currently available "second Committee Draft" of the proposed standard (see C++ Newsletter #020), you'll find some very curious wording in Clause 17.3.4.4: Throughout the C++ Library clauses (17 through 27), whenever a template member function is declared with one or more default arguments, this is to be understood as specifying a set of two or more overloaded template member functions. The version with the most parameters defines the interface; the versions with fewer parameters are to be understood as functions with fewer parameters, in which the corresponding default argument is substituted in-place. This "standards rewrite rule" is an example of the last-minute standards patching that goes on when both a language and a library are being aggressively designed at the same time, and then somebody discovers that the library depends upon a language feature that doesn't exist. Here's the problem: class A { public: A(); // has a default constructor }; A a; class B { public: B(int); // doesn't have a default constructor }; B b(19); template void f(T t1, T t2 = T()); // note the default argument void g() { f(a, a); // 1) ok, both arguments present f(a); // 2) ok, second argument defaults to A::A() f(b, b); // 3) ??? f(b); // 4) error, 2nd arg defaults to absent B::B() } The issue is when and where dependent default arguments of template functions get instantiated. Currently they get instantiated in the context of the declaration of the function, which means that the line 3) call above is an error, because B::B() is looked up and found not to exist. However significant parts of the new standard library have been written under the assumption that line 3) will compile, on the grounds that the default argument is not actually needed, and that only line 4) should cause an error. So what to do now? The problem could be worked around in the library by adding overloaded function signatures (as in the current draft's rewrite rule) or by using an additional intermediate template, but neither solution is concise or graceful, either for the standard library or for user-written classes in the years to come. On the other hand modifying the language to not instantiate default arguments unless needed involves the usual complexities of template instantiations and their context (see C++ Newsletter #016 and #017) and is a tricky change to make this late in the standards process. This inconsistency between language and library arose by mutual confusion and happenstance, which of course made the discussion about it at the last meeting especially overwrought, with no consensus reached. The question will probably be decided at the standards meeting next month, and we'll let you know what happens. INTRODUCTION TO STL PART 11 - REPLACING [snip] ACKNOWLEDGEMENTS Thanks to Nathan Myers, Eric Nagler, David Nelson, and Jonathan Schilling for help with proofreading. SUBSCRIPTION INFORMATION / BACK ISSUES To subscribe to the newsletter, send mail to majordomo@world.std.com with this line as its message body: subscribe c_plus_plus Back issues are available via FTP from: ftp.glenmccl.com /morespace1/glenmccl/newslett or on the Web at: http://www.glenmccl.com/~glenm There is also a Java newsletter. To subscribe to it, say: subscribe java_letter using the same majordomo@world.std.com address. ------------------------- Copyright (c) 1997 Glen McCluskey. All Rights Reserved. This newsletter may be further distributed provided that it is copied in its entirety, including the newsletter number at the top and the copyright and contact information at the bottom. Glen McCluskey & Associates Professional C++ Consulting Internet: glenm@glenmccl.com Phone: (800) 722-1613 or (970) 490-2462 Fax: (970) 490-2463 FTP: ftp.glenmccl.com /morespace1/glenmccl/newslett (for back issues) Web: http://www.glenmccl.com/~glenm