I wanted to make an exception class based on std::exception that allows me to give the file, function and line number using built-in macros (at least Visual Studio has them).
Because it's tedious to write these I made a simple #define to do it:
#define SOURCE __FILE__, __FUNCTION__, __LINE__
Is there any way I can achieve the same effect without using a macro such that the symbol remains in the Exceptions:: namespace? I don't much like polluting global.
Here's a repo for anyone who's interested:
#include <iostream>
#include <exception>
namespace Exceptions
{
class ExceptionBase : public std::exception
{
public:
explicit ExceptionBase() :
Line(0), File(nullptr), Function(nullptr) {}
explicit ExceptionBase(std::exception const & exception) :
std::exception(exception), Line(0), File(nullptr), Function(nullptr) {}
explicit ExceptionBase(char const * file, char const * function, unsigned line) :
File(file), Function(function), Line(line) {}
explicit ExceptionBase(char const * message, char const * file, char const * function, unsigned line) :
std::exception(message), File(file), Function(function), Line(line) {}
~ExceptionBase() {}
unsigned Line;
const char *File, *Function;
};
}
#define SOURCE __FILE__, __FUNCTION__, __LINE__
int main(void)
{
try
{
throw Exceptions::ExceptionBase(SOURCE);
}
catch(Exceptions::ExceptionBase const & base)
{
std::cout << "File: " << base.File << std::endl;
std::cout << "Function: " << base.Function << std::endl;
std::cout << "Line: " << base.Line << std::endl;
}
}