有与__builtin__FUNCTION()等效的MSVC吗

Is there MSVC equivalent for __builtin__FUNCTION()?

本文关键字:MSVC builtin FUNCTION 有与      更新时间:2023-10-16

根据这个答案,我们可以在GCC中使用__builtin__FUNCTION()找到调用函数的名称。这方面有MSVC的等效方案吗?

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");
}

是的,VS 2019 16.6 Preview 2(根据此链接(添加了这样的内部函数(__builtin_FILE()__builtin_FUNCTION()__builtin_LINE()__builtin_COLUMN()(,以支持c++20std::source_location