介于__func_和__PRETY_FUNCTION_之间
Something between __func__ and __PRETTY_FUNCTION__?
我使用g++4.8.1,并使用这两个宏进行调试。但是,__func__
宏只给了我函数名,如果在不同的类中有许多同名函数,这可能会产生误导。__PRETTY_FUNCTION__
宏生成整个函数签名,包括返回类型、类名和所有参数,这些参数可能很长。
我想要一个介于两者之间的宏,它只给我类名和函数名。有什么办法做到这一点吗?
受此启发,我创建了以下宏__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) + "(...)");
}
它的作用:
- 需要
__PRETTY_FUNCTION__
- 它删除返回类型和所有参数
- 如果函数的参数为零,则会附加
()
,否则为(...)
特点:
- 处理命名空间、构造函数等
- 也适用于括号运算符
限制:
- 它只适用于gcc
- 在运行时而非编译时创建
- 已分配堆
- 不适用于Lambda,
__FUNCTION__
和__PRETTY_FUNCTION__
不匹配。。。我几乎可以称之为编译器错误:)__FUNCTION__
看到operator()
__PRETTY_FUNCTION__
见<lambda(...)>
不幸的是,我认为这不容易做到。我不明白为什么从来没有人提出实现__CLASS__
宏,它可以扩展到当前类,例如,类似于GCC定义的所有宏。
我同意这些宏在一些困难的调试情况下是很有帮助的。可能难以实施。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- "error: no matching function for call to"构造函数错误
- int(c) 和 c-'0' 之间的区别。C++
- 调用专用模板时出错"no matching function for call to [...]"
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- "std::function<double(double)>"到"double (*)(double)"之间的转换
- boost::function 和 std::tr1::function 之间是否有重要的区别需要了解
- 为什么 GCC 的 std::function 不使用对按值传递的参数的右值引用来在其内部委托之间传递它们?
- std::function<> 和标准函数指针之间的区别?
- 在具有不同签名的 std::function 之间进行转换(T* arg 到 void* arg)
- 对象之间的差异->function()和object.function()