Consider this short program that I wrote:
#include <iostream>
template<bool Debug = false>
constexpr int add(const int& a, const int& b) {
if (Debug)
std::cout << __FUNCTION__ << " called on line " << __LINE__ << '\n';
return (a + b);
}
int main() {
std::cout << add(3, 7) << '\n';
std::cout << add<true>(5, 9) << '\n';
return 0;
}
It works just fine, and it gives the proper output:
10
add called on line 6
14
However, I'd like for the line number that is printed to be the line at the call site of the program which in this program should be line 12.
So how can I use __LINE__
or some other method to give me the line number from where the function was invoked?
The desired output would be:
10
add called on line 12
14
I would like for it to be generated from the function itself if possible.
-EDIT-
As a note to the reader, I'm open to any and all options but I am limited to C++17 for my current build environment and I'm using Visual Studio.