In current C++ I cannot think on a way to split the string at compile time. Most of the template tricks will not work on string literals. Now, I imagine that you want this to use in some sort of logging mechanism and you want to avoid the impact of performing the split at runtime in each method invocation. If that is the case, consider adding a function that will perform the operation and then in each function a static const std::string
to hold the value. That string will be initialized only once in the first call to the function:
#define DEFINE_LOG_NAME static const std::string _function_name( parse( __PRETTY_FUNCTION__ ) )
#define LOG_NAME( level ) do { DEFINE_LOG_NAME; log( level, _function_name ); } while (0)
std::string parse( std::string const & pretty ) {
// split here, return value
}
template <typename T>
struct B {
B() {
LOG_NAME( DEBUG );
}
};
(I have not tested this, so you might need to fiddle with it)
This will have some runtime impact, but only once for each function. Also note that this approach is not thread safe: if two threads simultaneously call a method that has not been called before there will be a race condition.