在下面的玩具示例中,我想獲取函數的名稱。該函數本身作爲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'不需要封裝任何名字...... –