views:

86

answers:

2

I have a string: B<T>::B() [with T = int]

Is there any way I can get B<T> [with T = int] from this before run time somehow? :)

EDIT:

Simplifying: Is there any way to get X & Y separately from a static string XY defined as a preprocessor macro in any form before runtime?

A: 

is this what you wanted ?

#define X "X"
#define Y "Y"
#define XY string(X) + Y
static string s = XY;
Lie Ryan
No. If you did it the other way - that is, decomposing `XY` to `X` and `Y` that would have been great.
nakiya
@nakiya: my point is, since you control the source code, the effect will be the same
Lie Ryan
+1  A: 

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.

David Rodríguez - dribeas