Template学习小记

来源:互联网 发布:大数据未来就业前景 编辑:程序博客网 时间:2024/06/05 00:57

14.1 Template parameter
2. There is no semantic difference between class and typename in a template-parameter.
//在其它地方typename可以指导编译器解释模版定义,告诉编译器哪些表达式是类型表达式(from c++ primer3ed)

4.A non-type template-parameter shall have one of the following types:
 integral or enumeration type.
 pointer to object or pointer to function.
 reference to object or reference to function.
 pointer to member.

6. a non-type non-reference template-parameter is not an lvalue.It shall not be assigned to or in any other way have its value change.
//非引用的非类型的模版参数,不能改变值和引用,但可以转换成临时变量(lvalue)被常量引用
7. a non-type template-paramter shall not be declared to have floating point,class,or void type.
//不理解floating point
9.A default template-argument shall not be specified in a function tmpelate declaration or a function template definition,nor in the template-parameter-list of the definition of a member of a class template.

14 A template-parameter cann't be used in preceding template-parameter or their default arguments.
//不能用在前述template-paramter中

14.2 Names of template specifications
4.When the name of a member template specification appear after . or -> in a postfix-expression,or after nested-name-specifier in a qualified-id,and the postfix-expression or qualified-id explicitly depends on a template-parameter,the member template name must be prefixed by the keyword template.
//一个模版类的成员函数被调用时,应有前缀template

14.3 Template arguments
2.In a template-argument,an ambiguity between a type-id and an expression is resovled to a type-id,regardless of the form of the corresponding template-parameter.

14.3.2 Template non-type arguments
2.A string literal is not an acceptable template-argument because a string literal is an object with internal linkage.
//可以定义一个字符串变量来完成
3.Address of array elements and names or address of non-static class members are not acceptable template-arguments.
14.3.3 Template template arguments
14.4 Type equivalence
14.5.1 Class templates
A class template defines the layout and operations for an unbounded set of related types.

14.5.5 Function template
A Function template defines an unbounded set of related functions.

////////////////模版的表示附问题
 template<class T=float,class Node=int> class Slack :public SingleNode<Node>
 {
 private:
  complex<T> _voltage;

 public:
  Slack(const std::string& strName,const complex<T>& voltage,Node nNode)
   :_voltage(voltage),SingleNode(strName,nNode)
  {}
 const complex<T>& Voltage() const
 {return _voltage;}
 };
在vc8编译错误:error C2226:syntax error : unexpected type 'std:string'
////而同样下面,确编译成功,似乎在编译器中认为T开头的才是parameter-declaration(模版参数),在C++98中也没有此规定啊,奇怪啊,并且要是没有派生类public singleNode<Node>,就是使用Node也不会出错啊
 template<class T=float,class T2=int> class Slack :public SingleNode<T2>
 {
 private:
  complex<T> _voltage;

 public:
  Slack(const std::string& strName,const complex<T>& voltage,T2 nNode)
   :_voltage(voltage),SingleNode(strName,nNode)
  {}
 const complex<T>& Voltage() const
 {return _voltage;}
 };

//////////////////////
//C++ primer 3rd 10.5 C++支持两种编译器模式:Inclusion Model和Separation Model
包含模式直接使用include
分离模式,函数模版的声明被放在头文件中,函数模版声明和定义的组织方式与程序中的非内联函数的声明和定义组织方式相同.定义中使用export
函数模版的显示实例化声明所在的文件中,函数模版的定义必须被给出
 

原创粉丝点击