在《C++ Primer》当中第六章提到了NDEBUG预处理宏,其中用到了cerr和__func__两个只是点,在此描述:
- NDEBUG
NDEBUG是一个预处理变量状态,如果在程序中定义了它,则assert等检查各种条件的宏不会执行,关闭了调试状态。 - __func__和cerr
string make_plural(size_t ctr, const string &word,
const string &ending = "s")
{
#ifndef NDEBUG
wcerr << __func__ << endl;
#endif
return (ctr > 1) ? word + ending : word;
}
这段程序返回程序中输入单词的复数形式,如果没有NDEBUG,就通过__func__这个编译器定义的局部静态变量(存放了函数名字)输出函数名。
其他几个名字:
wcerr << __FILE__ << endl; //存放文件名字符串字面值
wcerr << __LINE__ << endl; //存放当前行号整形字面值
wcerr << __TIME__ << endl; //文件编译时间字符串字面值
wcerr << __DATE__ << endl; //文件编译日期字符串字面值
对象 cerr 控件输出到流缓冲区与对象 stderr。
示例:
// iostream_cerr.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>
using namespace std;
void TestWide( )
{
int i = 0;
wcout << L"Enter a number: ";
wcin >> i;
wcerr << L"test for wcerr" << endl;
wclog << L"test for wclog" << endl;
}
int main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
TestWide( );
}
#define NDEBUG
using namespace std;
void show(vector<int> &v, int index) {
#ifndef NDEBUG
cout << "Addition information: " << endl;
wcerr << __func__ << endl;
wcerr << __FILE__ << endl; //存放文件名字符串字面值
wcerr << __LINE__ << endl; //存放当前行号整形字面值
wcerr << __TIME__ << endl; //文件编译时间字符串字面值
wcerr << __DATE__ << endl; //文件编译日期字符串字面值
#endif
if (index >= v.size()) { return; }
cout << v[index] << endl;
return show(v, ++index);
}
int main(int argc, char **argv)
{
vector<int> v{ 1,2,3,4,5 };
show(v, 0);
return 0;
}