Programming 版 (精华区)

发信人: netiscpu (说不如做), 信区: Programming
标  题: C++ 编程准则与忠告 之 Class Templates
发信站: 紫 丁 香 (Sun Jul 26 11:20:18 1998), 转信



8 Class Templates

Back to index 

Rec. 39 Do not attempt to create an instance of a class template
using a type that does not define the member functions which the class
template, according to its documentation, requires. 


Rec. 40 Take care to avoid multiple definition of overloaded
functions in conjunction with the instantiation of a class template. 


It is not possible in C++ to specify requirements for type arguments
for class templates and function templates. This may imply that the
type chosen by the user, does not comply with the interface as
required by the template. For example, a class template may require
that a type argument have a comparison operator defined. 

Another problem with type templates can arise for overloaded
functions. If a function is overload, there may be a conflict if the
element type appears explicitly in one of these. After instantiation,
there may be two functions which, for example, have the type int as an
argument. The compiler may complain about this, but there is a risk
that the designer of the class does not notice it. In cases where
there is a risk for multiple definition of member functions, this must
be carefully documented. 



Example 34 Problem when using parameterized types (Cfront 3.0 or
other template compiler) 

  template  

  class Conflict

  {

    public:

      void foo( int a );

      void foo( ET a );    // What if ET is an int or another integral type?

               // The compiler will discover this, but ...

  };

        


--

                              Enjoy Linux!
                          -----It's FREE!-----

※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: mtlab.hit.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.961毫秒