C++ 用现代C+替换宏+;

C++ 用现代C+替换宏+;,c++,c++20,C++,C++20,我有以下宏 #define Error(error_msg) ErrorMsg(__FILE__,__LINE__,error_msg) 我想知道这是否可以用更现代的C++来代替,它可以在头文件中一次性定义? 我的函数ErrorMsg有以下接口 void ErrorMsg(const std::string &file, int line, const std::string &report) 如评论中所说的“M88”,是现代C++方式获取文件名、函数名和行号——如此现代,

我有以下宏

 #define Error(error_msg) ErrorMsg(__FILE__,__LINE__,error_msg)
我想知道这是否可以用更现代的C++来代替,它可以在头文件中一次性定义?

我的函数
ErrorMsg
有以下接口

void ErrorMsg(const std::string &file, int line, const std::string &report)

如评论中所说的“M88”,是现代C++方式获取文件名、函数名和行号——如此现代,实际上它只支持支持C++ 20的新编译器。 下面是一个程序,它同时执行宏方式和std::source_位置方式,以便您可以比较它们:

#include <iostream>
#include <source_location>

void ErrorMsg(const std::string &file, int line, const std::string &message)
{
    std::cout << "info: " << file << ":" << line << ": " << message << "\n";
}

#define ErrorMacro(error_msg) ErrorMsg(__FILE__,__LINE__,error_msg)

void ErrorFunction(const std::string &message, const std::source_location& location = std::source_location::current())
{
    std::cout << "info: "
    << location.file_name() << "("
    << location.line() << ":"
    << location.column() << ") `"
    << location.function_name() << "` "
    << message << '\n';
}

int main()
{
    ErrorMacro("Hello World");    ErrorFunction("Hello World");

    return 0;
}
试试看

还有一个宏版本也可以打印函数名:

#include <iostream>
#include <source_location>

void ErrorMsg(const std::string &file, const std::string &function, int line, const std::string &message)
{
    std::cout << "info: " << file << "(" << line << ") `" << function << "` " << message << "\n";
}

#ifdef _MSC_VER
#define ErrorMacro(error_msg) ErrorMsg(__FILE__,__FUNCSIG__,__LINE__,error_msg)
#else
#define ErrorMacro(error_msg) ErrorMsg(__FILE__,__PRETTY_FUNCTION__,__LINE__,error_msg)
#endif

void ErrorFunction(const std::string &message, const std::source_location& location = std::source_location::current())
{
    std::cout << "info: "
    << location.file_name() << "("
    << location.line() << ":"
    << location.column() << ") `"
    << location.function_name() << "` "
    << message << '\n';
}

int main()
{
    ErrorMacro("Hello World");    ErrorFunction("Hello World");

    return 0;
}

请在

的C++20上试用,很遗憾。谢谢你的提示!!不知道
源位置
。谢谢@m88。我所能想到的一切都会丢失
\uuuuuu FILE\uuuuuuuu
\uuuuu LINE\uuuuuuu
的正确值。可能是一些神秘的伏都教模板,但我不擅长完全的伏都教。我认为std::source_位置是一条路,我将标记c++20,稍后检查我们是否可以升级@m88将
source\u location
能够检测从哪一行/文件/函数调用它,而不必显式地馈送它吗?下面是一个示例:
#include <iostream>
#include <source_location>

void ErrorMsg(const std::string &file, const std::string &function, int line, const std::string &message)
{
    std::cout << "info: " << file << "(" << line << ") `" << function << "` " << message << "\n";
}

#ifdef _MSC_VER
#define ErrorMacro(error_msg) ErrorMsg(__FILE__,__FUNCSIG__,__LINE__,error_msg)
#else
#define ErrorMacro(error_msg) ErrorMsg(__FILE__,__PRETTY_FUNCTION__,__LINE__,error_msg)
#endif

void ErrorFunction(const std::string &message, const std::source_location& location = std::source_location::current())
{
    std::cout << "info: "
    << location.file_name() << "("
    << location.line() << ":"
    << location.column() << ") `"
    << location.function_name() << "` "
    << message << '\n';
}

int main()
{
    ErrorMacro("Hello World");    ErrorFunction("Hello World");

    return 0;
}
info: ./example.cpp(27) `int main()` Hello World
info: ./example.cpp(27:62) `int main()` Hello World