Skip to content

Instantly share code, notes, and snippets.

@BI1BFR
Last active August 28, 2022 15:23
Show Gist options
  • Save BI1BFR/3221950a96154180467a to your computer and use it in GitHub Desktop.
Save BI1BFR/3221950a96154180467a to your computer and use it in GitHub Desktop.
c++11: calculate base name of __FILE__ at compile time
#include <iostream>
template<std::size_t Len>
constexpr const char* baseNameImpl(const char(&str)[Len], std::size_t pos)
{
return pos==0 ? str : (str[pos] == '/' || str[pos] == '\\') ? str+pos+1 : baseNameImpl(str, --pos);
}
template<std::size_t Len>
constexpr const char* baseName(const char(&str)[Len])
{
return baseNameImpl(str, Len-1);
}
int main()
{
std::cout << baseName(__FILE__) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment