std::experimental::source_location
will probably be added to the C++ standard at some point. I'm wondering if it possible to get the location information into the compile-time realm. Essentially, I want a function that returns different types when called from different source locations. Something like this, although it doesn't compile because the location
object isn't constexpr
as it's a function argument:
#include <experimental/source_location>
using namespace std::experimental;
constexpr auto line (const source_location& location = source_location::current())
{
return std::integral_constant<int, location.line()>{};
}
int main()
{
constexpr auto ll = line();
std::cout << ll.value << '\n';
}
This doesn't compile, with a message about
expansion of [...] is not a constant expression
regarding the return std::integral_constant<int, location.line()>{}
line. What good it is to have the methods of source_location
be constexpr
if I can't use them?
std::experimental::source_location
at all. Theline
function isconstexpr
, but its arguments are notconstexpr
. Function arguments are neverconstexpr
. In C++20, we may have arbitrary types as template arguments, so it's possible thesource_location
could be a template parameter – Salmonellasource_location
object is with a defaulted function argument. A defaulted template argument wouldn't work; it's created early, not late. This may be an oversight.constexpr
function arguments would fix the problem, but that idea isn't seen too favorably at this time – Salmonella