I know, that my question is similar to this: Passing the caller __FILE__ __LINE__ to a function without using macro
But I'm wondering, why there is no c++ equivalent of caller file and line passing to a function.
Having classes, templates, default arguments and more strict const-correctness makes a lot of macro usage obsolete, but when I want to pass caller file and line to a function, I will go in trouble for 2 reasons: 1. Default arguments (hard to replace all kinds of calls with a macro), 2. Macros break class member name scope. (startTimer macro would be global)
Thinking about the default arguments to functions, why are there no default-args-style replacements for FILE and LINE macros? It could be done like this:
void startTimer(int type, int id=0, string file = _CALLER_::FILE, int line = _CALLER_::LINE)
If the compiler, while looking for function calls of startTimer, can replace the id parameter with 0, where it isn't mentioned, it should be possible to replace file and line with the corresponding CALLER members, which in this example depend on the file, being processed, and the line where the call is made - something that the compiler knows while parsing a cpp file.
Knowing the caller of a member function may be useful for debugging - on which file/line has something been called, which leads to a problem later.
I think to have the feature optional, i.e. have it part of the function declaration the way you suggest, is not easy or not possible. The compiler must insert the needed information at, well, compile time (because file and line information are lost at run time), but at that time it may not know at all which function will be called in the case of virtual member functions or even plain old function pointers which get assigned in unpredictable ways.
But it would perhaps be possible to have all functions receive two hidden and undeclared parameters, the same way that member functions receive a hidden this pointer. In that scenario the compiler doesn't need to know which function is called because all functions receive the hidden caller line and file parameters.
Even if that is possible, I see the following problems:
If you consider that the functionality is available right now in the shape of a macro (with the only downside of name space pollution which is totally manageable), the benefit/effort ratio simply isn't good enough, is my guess.