根据此answer,我们可以在GCC中使用__builtin__FUNCTION()
找到调用函数的名称。是否有等效的MSVC?
答案 0 :(得分:6)
std::source_location
将来将成为跨平台解决方案,使您可以:
void log(const std::string& message, const std::experimental::source_location& location = std::experimental::source_location::current())
{
std::cout << location.function_name() << ": " << message << "\n";
}
int main()
{
log("test");
}
在此可用之前,我所知道的最佳解决方案是使用宏捕获__FUNCTION__
的值并将其传递给函数。例如这样的东西:
void log(const std::string& message, const std::string& function)
{
}
#define LOG(message) log(message, __FUNCTION__)
int main()
{
LOG("test");
}