A reasonably conforming version of std::experimental::source_location
can be implemented in gcc with __builtin_FILE()
, __builtin_LINE()
, etc. Do similar intrinsics exist in Visual Studio 2017? Or is there any way to implement std::experimental::source_location
in Visual Studio 2017?
-
There are macros defined in VS `__FILE__` `__LINE__` `__FUNCTION__` – drescherjm Jun 26 '17 at 18:29
-
4Those aren't sufficient to implement `source_location`. `__builtin_FILE()` returns the filename of the caller, not the raw location. It has be implemented by the compiler, not the preprocessor like those macros can be. – Mike Jun 26 '17 at 18:48
1 Answers
Unfortunately, at the moment there is no way to properly implement source_location only by means of compiler, so you'll have to use preprocessor and macros like __FILE__
, __LINE__
and __FUNCTION__
to feed the location info into the data structure that stores them (what you call source_location).
Having said that, I really do share your pain.
I have been recently developing a small library that adds location data and some other information to the exceptions that get thrown, and there I had to end up with an ugly macro like MY_THROW(Exception(args))
which basically just feeds the values obtained from the aforementioned macros into the function which throws an exception. As terribly-looking as it is, it seems to be the only working option so far.

- 1,894
- 1
- 14
- 25