C++ 模板名称漂亮打印 [英] C++ template name pretty print

查看:31
本文介绍了C++ 模板名称漂亮打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试目的,我需要打印缩进的模板名称.例如,我想像这样缩进名称而不是单行:

I have need to print indented template names for debugging purposes. For example, instead of single-line, I would like to indent name like this:

boost::phoenix::actor<
    boost::phoenix::composite<
      boost::phoenix::less_eval,
      boost::fusion::vector<
        boost::phoenix::argument<0>,
        boost::phoenix::argument<1>,

我开始自己编写,但变得越来越复杂.是否有现有的解决方案?

I started writing my own but is getting to be complicated. Is there an existing solution?

如果没有,你能帮我完成我的实施吗?如果是这样我会发布它.

if there is not one, can you help me to finish up my implementation? I will post it if so.

谢谢

这就是 typeid.name 的样子,

this is what typeid.name looks like,

boost::phoenix::actor<boost::phoenix::composite<boost::phoenix::less_eval, 
boost::fusion::vector<boost::phoenix::argument<0>, 
boost::phoenix::composite<boost::phoenix::multiplies_eval, 
boost::fusion::vector<boost::phoenix::argument<1>, boost::phoenix::argument<2>,
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void >, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, 
boost::fusion::void_> > >

这是我的目标

 6 boost::phoenix::actor<
 7   boost::phoenix::composite<
 8     boost::phoenix::less_eval,
 9     boost::fusion::vector<
10       boost::phoenix::argument<0>,
11       boost::phoenix::composite<
12         boost::phoenix::multiplies_eval,
13         boost::fusion::vector<
14           boost::phoenix::argument<1>,
15           boost::phoenix::argument<2>,
16           boost::fusion::void_,
17           boost::fusion::void_,
18           boost::fusion::void_,
19           boost::fusion::void_,
20           boost::fusion::void_,
21           boost::fusion::void_,
22           boost::fusion::void_,
23           boost::fusion::void >, // indentation messed up
24           boost::fusion::void_,
25           boost::fusion::void_,
26           boost::fusion::void_,
27           boost::fusion::void_,
28           boost::fusion::void_,
29           boost::fusion::void_,
30           boost::fusion::void_,
31           boost::fusion::void_
32         >
33       >
34     >

这样我就可以真正阅读声明

so that I can actually read declaration

推荐答案

当然不是最优雅的部分,但这应该能让你了解结束标签:

Certainly not the most elegant piece, but this should get you going regarding the closing tags:

std::string indent(std::string str, const std::string &indent = "  ") {
    std::string indent_ = std::string("\n");
    size_t token = 0;

    while ((token = str.find_first_of("<>,", token)) != std::string::npos) {
        switch(str[token]) {
            case '<': indent_.append(indent);
            case ',': str.insert(token + 1, indent_);
                      break;
            case '>': indent_.erase(indent_.size() - indent.size());
                      str.insert(token, indent_);
        }

        token += indent_.size() + 1;            
        const size_t nw = str.find_first_not_of(" ", token);
        if(nw != std::string::npos) {
            str.erase(token, nw-token);
        }
    }

    return str;
}

这篇关于C++ 模板名称漂亮打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆