I'm guessing the problem might be tied to my overall design, but I'd like to know why this behavior happens. Some background:
I have a logger class, which implements a number of << operators for common types.
namespace logging {
class log {
public:
...
_FRIEND _INLINE
logging::log& operator << (IN logging::log& l, IN void* v) {
LOG_APPEND_SPRINTF_MSG(32, "%p", v); return l;
}
...
};
}
There's a reason I singled out the overload for void*. My vector class provides an automatic cast operator to float*. In order to output a vector, however, I provide a special case for the << operator in global namespace:
_INLINE
logging::log& operator<<(logging::log& log, const math::vec3& v)
{
log << '(';
log.append_float(v.x);
log << ", ";
log.append_float(v.y);
log << ", ";
log.append_float(v.z);
log << ')';
logging::dispatch_log_message(log);
return log;
}
Now, if I log a vec3 in a function that lies in the global namespace, the proper operator for << gets called. However, when logging from a function that resides inside any namespace, Visual Studio first casts the vector to float* and then forwards that to the void* operator.
For now I only know one bad solution apart from rewriting my logging class - define the << operator for vec3 in the respective namespace (this has to be done for all namespaces that want to log a vector type).
namespace mynamespace {
logging::log& operator<<(logging::log& log, const math::vec3& v) ...
}
void mynamespace:foo(math::vec3& v){
log << v << endl;
}
This extremely annoying. What prompts this behavior/operator resolution? More importantly, can this be solved without expelling the base type operators from the logging class (I mean, frankly I haven't even tried, but for now I'm also using certain internal functions so I would need to restructure the entire logging class to boot; besides - I'd like to know if there's a simpler solution out of sheer principle )?