jeudi 21 juillet 2016

Determining calling line without macro

Is it possible to determine the line number that calls a function without the aid of a macro?

Consider this code:

#include <iostream>

#define PrintLineWithMacro() 
  std::cout << "Line: " << __LINE__ << std::endl;   // Line 4

void PrintLine()
{
  std::cout << "Line: " << __LINE__ << std::endl;   // Line 8
}

int main(int argc, char **argv)
{
  PrintLine();           // Line 13
  PrintLineWithMacro();  // Line 14
  return 0;
}

which outputs the following:

Line: 8
Line: 14

I understand why each prints what they do. I am more interested if it's possible to mimic the macro function without using a macro.

Aucun commentaire:

Enregistrer un commentaire