TITLE: template separate compilation and specialization [ There is an interesting article on templates in Glen's newsletter. For brevity, I removed the other articles. See the trailer for complete subscription info. -adc ] Issue #026 August, 1997 Contents: Dynamic Casts Introduction to Object-oriented Design Part 5 - Representation Hiding Notes From ANSI/ISO - Template Separate Compilation and Specialization Introduction to STL Part 13 - Accumulating DYNAMIC CASTS [snip] INTRODUCTION TO OBJECT-ORIENTED DESIGN PART 5 - REPRESENTATION HIDING [snip] NOTES FROM ANSI/ISO - TEMPLATE SEPARATE COMPILATION AND SPECIALIZATION Jonathan Schilling, jls@sco.com In C++ Newsletter #017 the new template separate compilation model was described. Since its introduction the ANSI/ISO standards committee has been dealing with sorting out the loose ends from this addition to the language. Take the following case, for instance: file1.h: template class A { }; template void f(T); file1.C: #include "file1.h" export template void f(T) { A a; } These files introduce a template class, and a template function whose definition depends upon the template class. The keyword "export" says that the template function definition doesn't have to be included into translation units that call it; rather it can be separately compiled. file2.C: #include "file1.h" template<> class A { }; // line 2 void g() ( f(1); } // line 3 Line 2 of this file declares a specialization (see C++ Newsletter #012) of the class template for type int, and then line 3 instantiates the function template for type int, which in turns instantiates the class template for type int. But the function template definition in file1.C cannot see the specialization in file2.C. So how does this work? Does the specialization not get used? Or does compilation of file1.C get deferred somehow until the specialization is seen? At the recent standards meeting in London, the committee decided that when an explicit specialization is not visible in an instantiation context, yet would affect the instantiation (such as in this case), the program is ill-formed, but that no diagnostic is required. This ruling falls into the same general behavior as the One Definition Rule (see C++ Newsletter #015). As such, it makes things easier for compiler implementors but harder for language users. For instance, the writer of file2.C may not look at the implementation of f(), and thus may not realize that specialization of class A will cause (possibly silent) undefined behavior. As we've editorialized before, the One Definition Rule is a weak area of C++, especially when compared to languages such as Ada and Java which stay well-defined across separate compilation boundaries. The problems mostly result from the use of #include files as the mechanism for modularity; improving this was something occasionally discussed in C++ standardization circles but never really tackled. Maybe in C++ 0X! INTRODUCTION TO STL PART 13 - ACCUMULATING [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