Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there MSVC equivalent for __builtin__FUNCTION()?

According to this answer we can find the name of the calling function using __builtin__FUNCTION() in GCC. Is there MSVC equivalent for this?

like image 846
Alexandru Irimiea Avatar asked Oct 31 '18 13:10

Alexandru Irimiea


2 Answers

Yes, such intrinsics (__builtin_FILE(), __builtin_FUNCTION(), __builtin_LINE(), __builtin_COLUMN()) were added for VS 2019 16.6 Preview 2 (according to this link) to support c++20 std::source_location.

like image 43
Ans Avatar answered Nov 12 '22 19:11

Ans


std::source_location will be the cross platform solution in the future allowing you to do:

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

Until this is available the best solution I am aware of is to use macros to capture the value of __FUNCTION__ and pass it to your function. For example something like this:

void log(const std::string& message, const std::string& function)
{
}

#define LOG(message) log(message, __FUNCTION__)

int main()
{
  LOG("test");
}
like image 171
Alan Birtles Avatar answered Nov 12 '22 20:11

Alan Birtles