特殊用途语言途径
1、默认实参
在函数的很多次调用中他们都被赋予一个相同的值,此时我们把这个反复出现的值成为函数的
默认实参
typedef string::size_type sz;
string screen(sz ht=24,sz wid=80,char backgrnd=' ');
使用默认实参调用函数
默认实参负责填补函数调用缺少的尾部实参(即靠右的位置)
2、内联函数和constexpr
由于调用函数效率会很低,而调用内联函数的话可以避免函数调用的开销
constexpr函数
constexpr函数式指能用于常量表达式的函数
遵守几条约定:
1、函数的返回类型及所有形参类型都得是字面值类型
2、函数体中有且只有一条return语句
把内联函数和constexpr函数放在头文件内
3、调试帮忙
assert和NDEBUG
assert预处理宏
assert(expr);
先对expr求值,如果表达式为假(即0),assert输出信息并终止程序的执行。如果表达式为真(即非零),assert什么也不做。
assert定义在cassert头文件中
assert(word.size() > threshold);
用来检测“不能发生的”条件
NDEBUG预处理变量
assert的行为依赖于一个名为NDEBUG的预处理变量的状态
void print(const int ia[], size_t size)
{
#ifndef NDEBUG
//_ _func_ _是编译器定义的一个局部静态变量,用于存放函数的名字
cerr<<_ _func_ _<<" : array size is "<<size<<endl;
#endif // NDEBUG
//...
}
还有一些编译器定义的一个局部静态变量
if(word.size() < threshold)
cerr<<"Error: "<<_ _FILE_ _
<<" : in function "<<_ _func_ _
<<" at line "<<_ _LINE_ _<<endl
<<" Compiled on "<<_ _DATE_ _
<<" Word read was \""<<word
<<"\": Length too short"<<endl;
如果程序提供了一个长度小于threshold的string对象,将得到下面的错误消息
Error:wdebug.cc : in function main at line 27
Complied on Jul 11 2012 at 20:50:03
word read was "foo": Length too short
PS:好吧这章很模糊,而且不容易懂,但是我还是硬着头皮基本是吧书本翻了一遍,有了一个大概的了解,以后有了更深入的了解了之后我会回来进行学习的
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)