每次打印调试数据都要写
printf("a:%d\n",a);
浪费时间,浪费生命!
初学c++,c++的模版真的NB
废话不多说,看代码
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
#define VARIABLE_NAME(var) #var
#define myprint(value) printToCString(VARIABLE_NAME(value),"\t: ",value);
template <typename... Args>
void printToCString(Args... args) {
stringstream ss;
(ss << ... << args);
printf("%s\n", ss.str().c_str());
}
int main()
{
int count = 123;
myprint(count);
int count2 = 1234;
myprint(count2);
int count3 = 12345;
myprint(count3);
char* myname = "海贼王";
myprint(myname);
float xiaoshu = 4.332;
myprint(xiaoshu);
bool buer = true;
myprint(buer);
return 0;
}
输出
count : 123
count2 : 1234
count3 : 12345
myname : 海贼王
xiaoshu : 4.332
buer : 1
优雅,永不过时!