调试:显示变量名和值

· · 个人记录

最开始是看到了宏的#用法中的一个演示:

#define show(x) cout<<(#x)<<" = "<<(x)<<"   ";

例如,调用int a = 10; show(a)就可以输出a = 10。

调试时非常好用,然后稍微扩充了一下,变成这样:

#define dbug_show_space(x) cout<<(#x)<<" = "<<(x)<<"   ";
#define dbug_show(x,end) cout<<(#x)<<" = "<<(x)<<(end);
#define dbug_show_decide(t1,t2,to,...) to
#define show(...) dbug_show_decide(__VA_ARGS__,dbug_show,dbug_show_space,...)(__VA_ARGS__)
#define show_arr(arr,len) cout << (#arr) << ": "; \
for(int __eggi = 0; __eggi <= (len); __eggi++) cout << arr[__eggi] << " "; cout << "\n";

使用show(a)输出a = 10,使用show(a," --- ")输出a = 10 ---。如果有int arr[5] = {0,1,4,2,3};这样一个数组,使用show_arr(arr,4)就能输出arr: 0 1 4 2 3,可以很直观地显示出需要的值。

不过这样还是比较麻烦。比如有 5 个变量要显示,show就得写 5 遍。容易想到可变模板一遍输出。新版本:

void showf(const char* names, const char* end)
{
    cout << end;
}
template<class thisT, class... otherT> 
void showf(const char* names, const char* end, thisT thiss, otherT... other)
{
    char* i;
    for (i = const_cast<char *>(names); (*i) != ',' && (*i) != '\0'; i++)
    {
        putchar(*i);
    }

    cout << " = " << thiss << "  ";
    showf(i+1,end,other...);
}
#define show(...) showf(#__VA_ARGS__,"\n", __VA_ARGS__) //由于要输出一串变量,所以不加指定结尾的功能,固定为"\n"

例如有int t1 = 2, t2 = 4, t3 = 5;三个变量,用show(t1,t2,t3);就可以输出t1 = 2 t2 = 4 t3 = 5,简化了代码。

但是这样还有一个问题:无法输出数组,因此这一部分还要用之前的show_arr。

我们可以对数组的输出进行特化。输出数组(代替之前的show_arr)的声明是这样的:

template<class thisT, class... otherT> 
void showf(const char* names, const char* end, thisT thiss[], otherT... other);

但是如何知道数组的长度呢?首先在使用时要求在数组参数的后面跟上一个整数,表示输出的长度。那么数组长度就是模板参数包other中的下一个参数。

template<class... otherT> 
int getlen(int thiss, otherT... other) {return thiss;}

同理在输出变量后可以判断下一个参数是不是数组,是数组则换行:

bool nxt_arr(){return 0;}
template<class thisT, class... otherT>
bool nxt_arr(thisT thiss, otherT... other){return 0;}
template<class thisT, class... otherT>
bool nxt_arr(thisT thiss[], otherT... other){return 1;}

然后,不能把长度作为输出的一部分。可以在输出数组的部分调用一个nxt函数:将字符指针names移到下一个逗号,但不输出任何内容。nxt结束时调用showf。

template<class thisT, class... otherT> 
void nxt(const char* names, thisT thiss, otherT... other)
{
    char* i = const_cast<char *>(names);
    for (; (*i) != ',' && (*i) != '\0'; i++);
    showf(i+1,other...);
}

另外,这些函数必须使用前置声明:先声明后定义。原因是输出变量的sh和输出数组的sh可能会互相调用。

完整代码如下:

#include <bits/stdc++.h>
#define _eggy_ using
#define _party_ namespace
_eggy_ _party_ std; _party_ llamn{

#define for_name char* i = const_cast<char *>(names);\
    for (; (*i) != ',' && (*i) != '\0'; i++)
#define vlt_tmp template<class thisT, class... otherT> 
void showf(const char* names); bool nxt_arr();
vlt_tmp bool nxt_arr(thisT thiss, otherT... other);
vlt_tmp bool nxt_arr(thisT thiss[], otherT... other);
vlt_tmp int getlen(thisT thiss, otherT... other);
vlt_tmp void showf(const char* names, thisT thiss, otherT... other);
vlt_tmp void nxt  (const char* names, thisT thiss, otherT... other);
vlt_tmp void showf(const char* names, thisT thiss[], otherT... other);
#define show(...) showf(#__VA_ARGS__, __VA_ARGS__); 

int main()
{
        #define fio 0
        #if fio
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
        #endif
        #undef fio

    int a[5] = {3,7,4,6,1}, b[5] = {3,9,6,2,5};
    show(t2,t1,a,4,b,4)
    return 0;
}

void showf(const char* names) {cout << "\n";}
vlt_tmp int getlen(thisT thiss, otherT... other){return thiss;}
vlt_tmp void showf(const char* names, thisT thiss, otherT... other)
{
    for_name putchar(*i);
    cout << " = " << thiss << "  ";
    if (nxt_arr(other...)) cout << "\n";
    showf(i+1,other...);
}
vlt_tmp void nxt(const char* names, thisT thiss, otherT... other)
{
    for_name; showf(i+1,other...);
}
vlt_tmp void showf(const char* names, thisT thiss[], otherT... other)
{
    for_name putchar(*i); cout << ": ";
    int len = getlen(other...);
    for(int j = 0; j <= len; j++)
        cout << thiss[j] << " ";
    cout << "\n";
    nxt(i+1,other...);
}bool nxt_arr(){return 0;}
vlt_tmp bool nxt_arr(thisT thiss, otherT... other){return 0;}
vlt_tmp bool nxt_arr(thisT thiss[], otherT... other){return 1;}
}signed main(){return llamn::main();}