According to this answer we can find the name of the calling function using __builtin__FUNCTION()
in GCC. Is there MSVC equivalent for this?
Asked
Active
Viewed 2,985 times
8

Alexandru Irimiea
- 2,513
- 4
- 26
- 46
2 Answers
9
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");
}

Alan Birtles
- 32,622
- 4
- 31
- 60
-
`__builtin_FUNCTION` is GCC support for `source_location` implementation! That is a really funny answer for a direct question! – Oliv Oct 31 '18 at 21:06
-
1@Oliv sorry I don't understand your comment? The OP was looking for a visual studio solution – Alan Birtles Oct 31 '18 at 21:09
-
Do you mean source_location is implemented in MSVC? – Oliv Oct 31 '18 at 21:18
2
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
.

Ans
- 527
- 5
- 9