__func__和__PRETTY_FUNCTION__之间的东西? [英] Something between __func__ and __PRETTY_FUNCTION__?
问题描述
我使用g ++ 4.8.1并使用这两个宏进行调试。但是, __ func __
宏只给出了函数名称,如果在不同的类中有许多具有相同名称的函数,这可能会引起误解。 __ PRETTY_FUNCTION __
宏产生整个函数签名 - 返回类型,类名和所有参数,可能很长。
我想介于两者之间 - 一个宏,它只给我类名和函数名。任何方式来实现这一目标?
我最近处理了这个问题。受这的启发,我创建了以下宏 __ COMPACT_PRETTY_FUNCTION __
:
std :: string computeMethodName(const std :: string&函数,const std :: string& prettyFunction);
#define __COMPACT_PRETTY_FUNCTION__ computeMethodName(__ FUNCTION __,__ PRETTY_FUNCTION __)。c_str()// c_str()是可选的
$ b std :: string computeMethodName(const std :: string& function,const std :: string& prettyFunction){
size_t locFunName = prettyFunction.find(function); //如果输入是构造函数,它将获得类名称的开始,而不是方法。这就是为什么后面我们必须搜索第一个括号
size_t begin = prettyFunction.rfind(,locFunName)+ 1;
size_t end = prettyFunction.find((,locFunName + function.length()); //添加函数.length()使得它更快,并且允许处理运算符括号!
if(prettyFunction [end + 1] ==')')
return(prettyFunction.substr(begin,end - begin)+());
else
return(prettyFunction.substr(begin,end - begin)+(...));
}
功能:
使用 __ PRETTY_FUNCTION __
$ b ()
,否则(...)
特征:
- 处理名称空间,构造函数等等
- 与圆括号运算符一起工作!
局限性:
- 它仅适用于gcc
- 在运行时创建而不是编译时
- 分配给堆
- 不适用于lambdas,
__FUNCTION __
和__ PRETTY_FUNCTION __
不匹配 ...我几乎把它称为编译器错误:)
-
__ FUNCTION __
看到运算符()
-
__ PRETTY_FUNCTION __
看到< lambda(...)>
-
I work with g++ 4.8.1 and use these two macros for debugging. However, the __func__
macro gives me only the function name, which might be misleading in the case you have many functions with the same name in different classes. The __PRETTY_FUNCTION__
macro produces the whole function signature - with return type, class name and all arguments, which can be very long.
I'd like to have something between - a macro, which will give me only class name and function name. Any way to achieve that?
I recently dealt with the problem. Inspired by this, I create the following macro __COMPACT_PRETTY_FUNCTION__
:
std::string computeMethodName(const std::string& function, const std::string& prettyFunction);
#define __COMPACT_PRETTY_FUNCTION__ computeMethodName(__FUNCTION__,__PRETTY_FUNCTION__).c_str() //c_str() is optional
std::string computeMethodName(const std::string& function, const std::string& prettyFunction) {
size_t locFunName = prettyFunction.find(function); //If the input is a constructor, it gets the beginning of the class name, not of the method. That's why later on we have to search for the first parenthesys
size_t begin = prettyFunction.rfind(" ",locFunName) + 1;
size_t end = prettyFunction.find("(",locFunName + function.length()); //Adding function.length() make this faster and also allows to handle operator parenthesys!
if (prettyFunction[end + 1] == ')')
return (prettyFunction.substr(begin,end - begin) + "()");
else
return (prettyFunction.substr(begin,end - begin) + "(...)");
}
What it does:
- It takes
__PRETTY_FUNCTION__
- It removes return type and all arguments
- If the function has zero arguments, it appends
()
, otherwise(...)
Features:
- Handles namespaces, constructors and so on
- Works also with the parenthesis operator!
Limitations:
- It only works with gcc
- Created at runtime rather than compile time
- Heap allocated.
- Does not work for lambdas,
__FUNCTION__
and__PRETTY_FUNCTION__
don't match... I would almost call it a compiler bug :)__FUNCTION__
sees anoperator()
__PRETTY_FUNCTION__
sees<lambda(...)>
这篇关于__func__和__PRETTY_FUNCTION__之间的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!