前几天看了下SFINAE,挺有启发,写了一个直接打印变量的宏
效果:
image.png
下面附上代码
//
// WBUtilMacro.hpp
// jsonTest
//
// Created by 王璟鑫 on 2023/3/30.
//
using namespace std;
#include <vector>
#include <iostream>
#pragma once
#define VBLOG(...)\
vblog(#__VA_ARGS__,__VA_ARGS__);
#if __APPLE__
#ifdef DEBUG
#define VBDLOG(...)\
VBLOG(__VA_ARGS__)
#else
#define VBDLOG(...)
#endif
#else
#ifdef NDEBUG
#define VBDLOG(...)
#else
#define VBDLOG(...)\
VBLOG(__VA_ARGS__)
#endif
#endif
vector<string> splitString(const string &str, char sep = ','){
std::vector<std::string> array;
std::string::size_type pos1, pos2;
pos1 = 0;
pos2 = str.find(sep);
while (std::string::npos != pos2)
{
array.push_back(str.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = str.find(sep, pos1);
}
if (pos1 != str.length())
array.push_back(str.substr(pos1));
return array;
}
//递归终止函数
template <class T>
string LOG(T &val){
return "非基本数据类型 ,";
}
string LOG(int &val){
return std::to_string(val);
}
string LOG(long &val){
return std::to_string(val);
}
string LOG(long long &val){
return std::to_string(val);
}
string LOG(unsigned &val){
return std::to_string(val);
}
string LOG(unsigned long &val){
return std::to_string(val);
}
string LOG(unsigned long long &val){
return std::to_string(val);
}
string LOG(float &val){
return std::to_string(val);
}
string LOG(double &val){
return std::to_string(val);
}
string LOG(long double &val){
return std::to_string(val);
}
string LOG(string &val){
return (val);
}
string LOG(const char* &val){
return val;
}
template <class T, class ...Args>
string LOG(T &val, Args... args){
return "非基本数据类型 ," + LOG(args...);
}
template < class ...Args>
string LOG(int &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(long &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(long long &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(unsigned &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(unsigned long &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(unsigned long long &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(float &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(double &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(long double &val, Args... args){
return std::to_string(val) + "," + LOG(args...);
}
template < class ...Args>
string LOG(string &val, Args... args){
return val + "," + LOG(args...);
}
template < class ...Args>
string LOG(const char* &val, Args... args){
string a = val;
return a + "," + LOG(args...);
}
template <class ...Args>
void vblog(string names,Args...args){
auto nameArr = splitString(names);
auto valueArr = splitString(LOG(args...));
string final = "";
if (nameArr.size() != valueArr.size()) {
printf("打印函数出bug啦");
return;
}
for(int i = 0; i < nameArr.size(); ++i){
final.append(nameArr[i]);
final.append(" = ");
final.append(valueArr[i]);
final.append(" ");
}
cout << final << endl;
}
好用
网友评论