免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1340 | 回复: 6
打印 上一主题 下一主题

How To Organize Template Source Code [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-16 21:59 |只看该作者 |倒序浏览
How To Organize Template Source Code
By Nemanja Trifunovic
Different ways to organize source code in C++ template libraries  

Introduction
Often I get asked whether programming with templates is hard or easy. The answer I usually give is: "It is easy to use templates, but it is hard to make them". Just take a look at some template libraries that we use in our everyday programming, like STL, ATL, WTL, some libraries from Boost, and you will see what I mean by this. Those libraries are great example of the principle "simple interface - complex implementation".

I started using templates five years ago when I discovered MFC template containers, and until last year I had no need to develop them myself. When I finally got to the point that I needed to develop some template classes, the first thing that hit me was the fact that the "traditional" way of organizing source code (declarations in *.h files, and definitions in *.cpp files) does not work with templates. It took me some time to understand why this is the case, and how to work around this problem.

This article is aimed at developers who understand templates well enough to use them, but are not very experienced at developing them. Here, I will cover only template classes and not template functions, but the principles are the same in both cases.

The Problem Described
To illustrate the problem, we will use an example. Suppose we have a template class array (nothing to do with boost::array template class) in a file array.h.

  1. // array.h
  2. template <typename T, int SIZE>;
  3. class array
  4. {
  5.     T data_[SIZE];
  6.     array (const array& other);
  7.     const array& operator = (const array& other);
  8. public:
  9.     array(){};
  10.     T& operator[](int i) {return data_[i];}
  11.     const T& get_elem (int i) const {return data_[i];}
  12.     void set_elem(int i, const T& value) {data_[i] = value;}
  13.     operator T*() {return data_;}      
  14. };            
复制代码

            
Also, we have a file main.cpp in which is the code that uses array:

  1. // main.cpp
  2. #include "array.h"

  3. int main(void)
  4. {
  5. array<int, 50>; intArray;
  6. intArray.set_elem(0, 2);
  7. int firstElem = intArray.get_elem(0);
  8. int* begin = intArray;
  9. }
复制代码

        
This compiles fine, and does exactly what we want: first we make an array of 50 integers, then set the first element to be 2, read the first element, and finally take the pointer to the beginning of the array.

Now, what happens if we try to organize the code in more traditional way? Let's try to split the code in array.h and see what happens. Now we have two files: array.h and array.cpp (main.cpp remains unchanged).

  1. // array.h        
  2. template <typename T, int SIZE>;
  3. class array
  4. {
  5.       T data_[SIZE];
  6.       array (const array& other);
  7.       const array& operator = (const array& other);
  8.   public:
  9.       array(){};
  10.       T& operator[](int i);
  11.       const T& get_elem (int i) const;
  12.       void set_elem(int i, const T& value);
  13.       operator T*();      
  14. };        
  15.    
  16. // array.cpp
  17. #include "array.h"

  18. template<typename T, int SIZE>;
  19.        T& array<T, SIZE>;::operator [](int i)
  20.     {
  21.     return data_[i];
  22.     }

  23. template<typename T, int SIZE>;
  24.        const T& array<T, SIZE>;::get_elem(int i) const
  25.     {
  26.     return data_[i];
  27.     }

  28. template<typename T, int SIZE>;
  29.        void array<T, SIZE>;::set_elem(int i, const T& value)
  30.     {
  31.     data_[i] = value;
  32.     }
  33. template<typename T, int SIZE>; array<T, SIZE>;::operator T*()
  34.     {
  35.     return data_;
  36.     }
复制代码

      
Try to compile this, and you will get three linker errors. The questions are:

Why are these errors reported in the first place?
Why there are only three linker errors? We have four member functions in array.cpp.
To answer these questions, we will need to dig into a little more details about the template instantiation process.

Template Instantiation
One of the mistakes programmers usually make when they work with template classes is to treat them as types. The term parameterized types which is often used for template classes certainly does lead us to think this way. Well, template classes are not types, they are just what the name suggests: templates. There are several important concepts to understand about the relation between template classes and types:

  1. Compiler uses template classes to create types by substituting template parameters, and this process is called instantiation.
  2. The type that is created from a template class is called a  specialization.
  3. Template instantiation happens on-demand, which means that the compiler will create the specialization when it finds its use in code (this place is called point of instantiation).
  4. To create a specialization, compiler will need to "see" not only the declaration of the template in the point of instantiation, but also the definition.
  5. Template instantiation is lazy, which means that only the definitions of functions that are used are instantiated.
复制代码


If we go back to our example, array is a template, and array<int, 50>; is a template specialization - a type. The process of creating array<int, 50>; from array is instantiation. The point of instantiation is in the file main.cpp. If we organize the code in the "traditional" way, compiler will see the declaration of the template (array.h), but not the definition (array.cpp ). Therefore, compiler will not be able to generate the type array<int, 50>;. However, it will not report an error: it will assume that this type is defined in some other compilation unit, and leave it to linker to resolve.

Now, what happens with another compilation unit (array.cpp)? Compiler will parse the template definition and check for syntax correctness, but it will not generate the code for the member functions. How it could? In order to generate the code, compiler will need to know template parameters - it needs a type, not a template.

Therefore, linker will find the definition for array<int, 50>; neither in main.cpp nor in array.cpp and therefore it will report an error for all unresolved member definitions.

OK. That answers the question 1. But what about question 2? We have four member functions defined in array.cpp, and only three error messages reported by linker. The answer is in the concept of lazy instantiation. In main.cpp we don't use operator[] and compiler never even tried to instantiate its definition.

Solutions
Now that we understand what the problem is, it would be nice to offer some solutions. Here they are:

  1. Make the template definition visible to compiler in the point of instantiation.
  2. Instantiate the types you need explicitly in a separate compile unit, so that linker can find it.
  3. Use keyword export.
复制代码


The first two are often called inclusion model, while the third is sometimes referred as separation model.

The first solution really means that we need to include not only template declarations, but also the definitions in every translation unit in which we use the templates. In our example it means that we will use the first version of array.h with all member functions inlined, or that we include array.cpp  in our main.cpp. In that case, compiler will see both the declaration and definition of all member functions from array and it will be able to instantiate array<int, 50>;. The drawback of this approach is that our compilation units can become huge, and it can increase build and link time significantly.

Now the second solution. We can explicitly instantiate the template for the types we need. It is best to keep all explicit instantiation directives in a separate compilation unit. In our example, we can add a new file templateinstantiations.cpp

  1. // templateinstantiations.cpp               
  2. #include "array.cpp"

  3. template class array <int, 50>;; // explicit instantiation        
复制代码

        
Type array<int, 50>; will be generated not in main.cpp but in templateinstantiations.cpp and linker will find its definition. With this approach, we don't have huge headers, and hence the build time will drop. Also, the header files will be "cleaner" and more readable. However, we don't have the benefits of lazy instantiation here (explicit instantiation generates the code for all member functions), and it can become tricky to maintain templateinstantiations.cpp for big projects.

The third solution is to mark the template definitions with the keyword export and the compiler will take care about the rest. When I read about export in the Stroustrup book, I was very enthusiastic about it. It took me several minutes to find out that it was not implemented on VC 6.0, and a little more to find out that no compiler supported this keyword at all (the first compiler that supports this keyword was released in late 2002). Since then, I have read more about export and learnt that it hardly solves any of the problems encountered with the inclusion model. For more information about issues with this keyword, I recommend articles by Herb Sutter.

Conclusion
In order to develop template libraries, we need to understand that template classes are not "ordinary types" and that we need to think differently when working with them. The purpose of this article was not to scare the developers who want to do some template programming. On the contrary, I hope it will help them to avoid some usual mistakes that people who start template development usually make.

Literature
Bjarne Stroustrup: "The C++ Programming Language", Addison-Wesley Pub Co; ISBN: 0201889544 ; 3rd edition (June 20, 1997)
David Vandevoorde, Nicolai M. Josuttis: "C++ Templates: The Complete Guide", Addison Wesley Professional; ISBN: 0201734842 ; 1st edition (November 12, 2002)


原文地址:http://www.codeproject.com/cpp/templatesourceorg.asp
译文地址:http://www.allaboutprogram.com/viewtopic.php?t=2097

这几天正好有同学问我关于这个方面的问题,我原来tamplate也没学好,所以就在网上搜了一下,就找到了这篇文章。整篇看下来正好能解决我的疑问,所以就一时兴起,把它翻译成了中文。
本人初次尝试翻译,还希望各位多多指正,呵呵   

论坛徽章:
0
2 [报告]
发表于 2004-07-16 22:01 |只看该作者

How To Organize Template Source Code

声明,那个“我”不是我,是zweily。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2004-07-16 22:03 |只看该作者

How To Organize Template Source Code

晕~~~~

那为什么不贴中文的上来啊?
成心考我们啊?

看不懂原文~~~~
还好我早就知道了。

论坛徽章:
0
4 [报告]
发表于 2004-07-16 22:47 |只看该作者

How To Organize Template Source Code

希望楼主贴出中文的

论坛徽章:
0
5 [报告]
发表于 2004-07-16 23:52 |只看该作者

How To Organize Template Source Code

唉,最后我不是附了两个url了吗?我也没兴趣再回去贴一遍了,反正abp也不需要注册,你们自己看一眼吧。

论坛徽章:
0
6 [报告]
发表于 2004-07-17 07:47 |只看该作者

How To Organize Template Source Code

[quote]原帖由 "我不懂C++"]唉,最后我不是附了两个url了吗?我也没兴趣再回去贴一遍了,反正abp也不需要注册,你们自己看一眼吧。[/quote 发表:


楼主好像在推介你的aba吧,呵呵。我去看过。

论坛徽章:
0
7 [报告]
发表于 2004-07-17 15:04 |只看该作者

How To Organize Template Source Code

原帖由 "windflowers1976" 发表:


楼主好像在推介你的aba吧,呵呵。我去看过。

也可以这样说,反正也不冲突。至于abp,那可不是我的,那是老大的。
其实我后面是懒得贴了,否则我会转过来,然后给一个url。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP