将编译器生成的C ++ lambda名称更改为易于阅读的名称 [英] Change compiler-generated C++ lambda name to human-readable one

查看:46
本文介绍了将编译器生成的C ++ lambda名称更改为易于阅读的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,lambda表达式名称由编译器自动生成(至少在VCC编译器上如下所示): lambda_cf01cxf ... .

In C++ lambda expression names are generated by compiler automatically giving (at least on VCC compiler it looks like this: lambda_cf01cxf....

有没有什么方法可以将生成的名称更改为更易理解的名称?

Is ther any (doesn't matter if even non-portable) way to change this generated name to something more human readable?

推荐答案

这是创建命名的lambda的一种方法

Here is a way to create a named lambda

#include <iostream>
#include <type_traits>

using namespace std;

#define CREATE_NAMED_LAMBDA(name, lambda) \
[](auto&& fun)\
{\
    using lambdaType = typename decay<decltype(fun)>::type;\
    struct lambda_ ## name : lambdaType\
    { using lambdaType::operator(); };\
    return lambda_ ## name{forward<decltype(fun)>(fun)};\
}(lambda)

int main()
{
    auto lamb = CREATE_NAMED_LAMBDA(my, [](int i) { cout << i; });
    lamb(7);
}

在非调试版本和许多其他改进中,可以使宏名称更小,使其为空(仅传递lambda).但是有一个陷阱:该代码对您没有帮助.至少暂时是因为它使最新的MSVC编译器崩溃.可以在那里.

You can make the macro name smaller, make it empty (just passing lambda) in non-debug builds and many other improvements. There is a catch, though: this code won't help you. At least for the time being because it crashes the latest MSVC compiler. The bug report can be tracked there.

这篇关于将编译器生成的C ++ lambda名称更改为易于阅读的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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