3 ms·
I think I don't understand the post or problem. How can't you use `constexpr` computation to get the file name at compile time? #include <stdio.h> #incl
by dataflow 3y ago
I think I don't understand the post or problem. How can't you use `constexpr` computation to get the file name at compile time?
#include <stdio.h>
#include <array>
#include <source_location>
#include <string_view>
template<size_t N> constexpr auto GetFilename(char const name[]) {
std::array<char, N + 1> a = {};
std::char_traits<char>::copy(&a[0], name, N);
return a;
}
template<auto Value = GetFilename<
std::char_traits<char>::length(std::source_location::current().file_name())
>(std::source_location::current().file_name())>
using SL = std::integral_constant<decltype(Value), Value>;
int main() {
printf("%s\n", &SL<>::value[0]);
}
- menaerus 3y agoMe neither. Unless I am misunderstanding the issue in the post, a simple static constexpr std::string_view filename = std::source_location::current().file_name(); static_assert(filename == "whatever_the_name_of_file_is"); static_assert(filename.length() == 28); will do. Both the filename and filename length is available in the compile-time.
- gpderetta 3y agoIn your example, the source location will be for the template definition, instead of the function call. But yes, although you can't use std::source_location directly, you can roll your own. See my other comments elsethread.
- dataflow 3y agoAhhh, I understand now. Thanks!