macros - C++ - style passing caller file and line to function (equivalent of __FILE__ and __LINE__) -


i know, question similar this: passing caller __file__ __line__ function without using macro

but i'm wondering, why there no c++ equivalent of caller file , line passing function.

having classes, templates, default arguments , more strict const-correctness makes lot of macro usage obsolete, when want pass caller file , line function, go in trouble 2 reasons: 1. default arguments (hard replace kinds of calls macro), 2. macros break class member name scope. (starttimer macro global)

thinking default arguments functions, why there no default-args-style replacements file , line macros? done this:

void starttimer(int type, int id=0, string file = _caller_::file, int line = _caller_::line) 

if compiler, while looking function calls of starttimer, can replace id parameter 0, isn't mentioned, should possible replace file , line corresponding caller members, in example depend on file, being processed, , line call made - compiler knows while parsing cpp file.

knowing caller of member function may useful debugging - on file/line has been called, leads problem later.

i think have feature optional, i.e. have part of function declaration way suggest, not easy or not possible. compiler must insert needed information at, well, compile time (because file , line information lost @ run time), @ time may not know @ function called in case of virtual member functions or plain old function pointers assigned in unpredictable ways.

but perhaps possible have all functions receive 2 hidden , undeclared parameters, same way member functions receive hidden pointer. in scenario compiler doesn't need know function called because all functions receive hidden caller line , file parameters.

even if possible, see following problems:

  1. that's major change implementation, , language. (i'm not sure whether done behind scenes implementation still claims comply today's standard.) impossible in c practical purposes because break existing abis (unless microsoft ;-) ). less of issue in c++ abi compatibility worse anyway.
  2. it may performance problem 2 parameters need passed each function although aren't needed @ all, , may perhaps memory problem on tiny (e.g. embedded) systems because stack grow faster. (of course claim shouldn't build such deep stack begin with, still.)

if consider functionality available right in shape of macro (with downside of name space pollution totally manageable), benefit/effort ratio isn't enough, guess.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -