Is it possible to print the caller source file name calling a function defined in another file, without passing __FILE__
explicitly and without using preprocessor tricks?
// Header.h
#include <iostream>
#include <string>
using namespace std;
void Log1(string msg) {
cout << __FILE__ << msg << endl; // this prints "Header.h"
}
void Log2(string file, string msg) {
cout << file << msg << endl;
}
inline void Log3(string msg) {
cout << __FILE__ << msg << endl; // this prints "Header.h"
}
// Source.cpp
#include "Header.h"
int main()
{
Log1(" Test 1");
Log2(__FILE__, " Test 2");
Log3(" Test 3");
}
With this code, this is what I get:
pathTo\Header.h Test 1
pathTo\Source.cpp Test 2
pathTo\Header.h Test 3
I would have expected the last call to print: pathTo\Source.cpp Test 3
inline
. It means "there can be multiple definitions in multiple Translation Units". That means it's intended for the linker. The preprocessor doesn't even notice the token. – Knave