4 ms·
`std::source_location` does not have to be a function argument. It can also be used in an NSDMI, in which case it ends up being instantiated whenever the NSDMI
by ynik 3y ago
`std::source_location` does not have to be a function argument.
It can also be used in an NSDMI, in which case it ends up being instantiated whenever the NSDMI is used, i.e. at every aggregate initialization site.
https://godbolt.org/z/v5rhjqdbY https://godbolt.org/z/v5rhjqdbY
Of course, that doesn't really allow you to do anything that you couldn't already do in the with a constexpr function (see below). In particular, such a struct can be used as a non-type template parameter, but a `template<NSDMI sloc = NSDMI{}> void foo();` will merely report the line where the template is defined, not where `foo` is called.
// Results in a compile-time constant, so can be used everywhere where __LINE__ could be used.
constexpr int line(std::source_location loc = std::source_location::current()) {
return loc.line();
}
So std::source_location can fully replace __FILE__/__LINE__, but it cannot always replace the macro where __FILE__/__LINE__ were being used.
- foota 3y agoHuh, being able to see where a template was instantiated is niche but kind of interesting.