1

I'm trying to write a class that logs calls to its operator= but I don't know of away to do it without changing the calling code. For a function whose signature I could adjust, something like this might work: How to know what function called another, but I don't see how that would work for a function like operator=. Is this possible?

Example class/usage:

template <typename T>
class LoggingT {
    T data;
    LoggingT& operator=(const LoggingT& rhs){
        data = rhs; 
        std::cout << "assigned at line: " 
          << ???? << " in file " << ???? << std::endl;
    return *this;
};
Community
  • 1
  • 1
Ben Jones
  • 919
  • 1
  • 8
  • 22
  • You might be interested in this: [C (programming language): Is there a way to print the name of the calling function in C without passing it as an argument?](http://www.quora.com/C-programming-language/Is-there-a-way-to-print-the-name-of-the-calling-function-in-C-without-passing-it-as-an-argument) – Cornstalks Aug 18 '14 at 16:16
  • Some operating systems allow you to get the call stack. See also [here](http://stackoverflow.com/questions/3899870/print-call-stack-in-c-or-c) please. – πάντα ῥεῖ Aug 18 '14 at 16:17
  • 1
    FWIW, there's a [proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3972.pdf) that apparently got favourable reviews. – chris Aug 18 '14 at 16:20
  • I don't see how the call stack is associated with function names. Even if you could get the address of the jump, how does one calculate the function starting address or function name without a map? – Thomas Matthews Aug 18 '14 at 16:24
  • @Thomas: Depending on the binary format of the executable (and how much info has been stripped out of it) this could be done by looking in the symbol table of the executable (ELF) or equivalent, then demangling the name using a compiler-specific library. – Cameron Aug 18 '14 at 16:31
  • You'd also have to wonder how this would interact with e.g. `std::fill`, which has a pretty good idea how it's going to call `operator=`. That is to say, not with extra arguments. – MSalters Aug 18 '14 at 16:39
  • @ThomasMatthews The [answer](http://stackoverflow.com/a/3899916/1413395) of the Q&A link I gave, mentions the [`backtrace_symbols()`](http://linux.die.net/man/3/backtrace_symbols) function, to get the actual function names for the addresses retrieved with `backtrace()`. – πάντα ῥεῖ Aug 18 '14 at 16:39
  • Unfortunately, the backtrace_symbols() is not in every compiler. – Thomas Matthews Aug 18 '14 at 16:58
  • @ThomasMatthews As I mentioned _Some operating systems_ :P ... – πάντα ῥεῖ Aug 18 '14 at 17:03

0 Answers0