我正在尝试编写日志记录功能,该功能将打印对象ID(如果存在)。VS 2019可以编译并正常运行,但Clang失败。如何为Clang工作?
注意:“ log”成员可以存在或不存在-取决于类开发人员。它是可选的。全局“ log”变量将始终存在。
简短示例:
int i = 0;
struct S {
int i = 1;
int f() {
return i;
}
static int g() {
return [&](){
return i;
}();
}
};
Run Code Online (Sandbox Code Playgroud)
完整的例子
#include <cstdio>
#include <cassert>
#include <string>
using std::string;
#define LOG(x) [&]() \
{ \
log.print(x); \
} \
();
namespace myspace {
class Log
{
public:
Log(string id)
: m_Id(id)
{
}
void print(const string& str)
{
printf("%s %s\n",m_Id.c_str(),str.c_str());
}
private:
string m_Id;
};
Log …Run Code Online (Sandbox Code Playgroud)