在下面的玩具示例中,我想获取函数的名称。该函数本身作为std::function
参数给出。在C++中可以获得std::function
对象的名称吗?获取std :: function的名称
void printName(std::function<void()> func){
//Need a function name()
std::cout << func.name();
}
void magic(){};
//somewhere in the code
printName(magic());
output: magic
否则,我将不得不将该函数的名称作为第二个参数。
是这个例子完整?难道你只是使用预处理器TOSTRING将魔法转换为字符串? printName(TOSTRING(magic)) – thorsan
这是不可能的。一旦编译完成,你会失去所有这些信息,C++没有任何[类型自省](https://en.wikipedia.org/wiki/Type_introspection)。唯一的可能性是依赖于编译器,使用调试信息来尝试找出它。 –
'std :: function'不需要封装任何名字...... –