Last active
August 28, 2022 15:23
-
-
Save BI1BFR/3221950a96154180467a to your computer and use it in GitHub Desktop.
c++11: calculate base name of __FILE__ at compile time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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