96 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			96 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
#### 2.1.2 __func__
 | 
						||
    __func__ 宏:返回函数名称 c++11兼容c99标准
 | 
						||
#### 2.1.4 变长参数宏以及__VA_ARGS__
 | 
						||
```
 | 
						||
#define LOG(...){\
 | 
						||
    fprintf(stderr,"%s: Line %d:\t",__FINE__,__LINE__);\
 | 
						||
    fprintf(stderr,__VA_ARGS__);\
 | 
						||
    fprintf(stderr,"\n");\
 | 
						||
}
 | 
						||
 | 
						||
int main(){
 | 
						||
    int x=3;
 | 
						||
    LOG("x=%d",x);//2-1-5.cpp: line 12:    x=3
 | 
						||
}
 | 
						||
```
 | 
						||
#### 2.4 宏__cplusplus
 | 
						||
可以用于判断是否是c++编译环境,同时也可以判断c++版本,防止c++11程序在c++98编译器下编译
 | 
						||
#### 2.5.1
 | 
						||
assert宏定义在cassert头文件中
 | 
						||
```
 | 
						||
assert(expr);
 | 
						||
```
 | 
						||
首先对expr求值,如果表达式为假,assert输出信息,并且终止程序运行。为真则什么也不做。
 | 
						||
 | 
						||
在开头使用(需要写在cassert头文件之前,因为本质上还是宏判断)
 | 
						||
```
 | 
						||
#define NDEBUG
 | 
						||
```
 | 
						||
可以跳过检测。在VC++里面,release会在全局定义NDEBUG。<br>
 | 
						||
既然定义了NDEBUG,还可以:
 | 
						||
```
 | 
						||
#ifndef
 | 
						||
    //do any debug thing
 | 
						||
    //__FILE__ 文件名字面值
 | 
						||
    //__LINE__ 当前行号字面值
 | 
						||
    //__TIME__ 文件编译时间字面值
 | 
						||
    //__DATE__ 文件编译时间字面值
 | 
						||
    //__func__ 函数名称 c++11兼容c99标准
 | 
						||
    //用于打印debug信息
 | 
						||
#endif
 | 
						||
```
 | 
						||
## error用法  
 | 
						||
```
 | 
						||
#ifndef UNIX
 | 
						||
#error This software requires the UNIX OS.
 | 
						||
#endif
 | 
						||
 | 
						||
#if !defined(__cplusplus)  
 | 
						||
#error C++ compiler required.  
 | 
						||
#endif  
 | 
						||
```
 | 
						||
#### 2.5.2 静态断言与static_assert
 | 
						||
编译期断言
 | 
						||
static_assert(false,"error string");
 | 
						||
#### 2.6 noexcept修饰符和noexcept操作符
 | 
						||
```
 | 
						||
void excpt_func() noexcept;//虽然这样函数就不会抛出异常,但是如果抛出了异常,程序会直接被终止
 | 
						||
 | 
						||
void excpt_func() noexcept(expr);//如果为true则不会抛出异常
 | 
						||
```
 | 
						||
#### 2.9 扩展的friend语法
 | 
						||
```
 | 
						||
class A{
 | 
						||
    
 | 
						||
};
 | 
						||
class B{
 | 
						||
  friend class A;//都通过 
 | 
						||
};
 | 
						||
class C{
 | 
						||
  friend B;//c++11通过,c++98失败  
 | 
						||
};
 | 
						||
```
 | 
						||
可以省略class直接声明类的友元,这个改动的意义在于类模板,不过使用内置对象时就会实例化成一个没有友元的类
 | 
						||
```
 | 
						||
class A;
 | 
						||
template <class T>
 | 
						||
class People{
 | 
						||
  friend T;  
 | 
						||
};
 | 
						||
```
 | 
						||
#### 模板函数的默认模板参数
 | 
						||
```
 | 
						||
template<typename T=int>
 | 
						||
void func(){
 | 
						||
    
 | 
						||
}
 | 
						||
```
 | 
						||
#### 2.12.1 外部模板
 | 
						||
在头文件中定义模板类的情况下,为了防止编译器给每个包含头文件的cpp文件生成代码,可以添加extern关键字。当然至少要有一个是不加extern的。
 | 
						||
```
 | 
						||
template <typename T> void func(T){
 | 
						||
    
 | 
						||
}
 | 
						||
 | 
						||
extern template void func<int>(int);
 | 
						||
``` |