我在使用我所做的#define 时遇到了问题。由于某种原因,它没有检查语句中的所有参数。
THROW_AND_LOG(Foo::Initialize() && Bar::Initialize() && FooBar::Initialize(), "Something() could not Initialize singletons.");
出于某种原因,它只是初始化 Foo 然后退出语句。它不会抛出或记录。
但是
THROW_AND_LOG((Foo::Initialize() && Bar::Initialize() && FooBar::Initialize()), "Something() could not Initialize singletons.");
出于某种原因工作正常。 (将所有支票包裹在括号中)。
我将把所有内容都移到它自己的单独行以解决这个问题,因为它首先不是最好的代码。但是我很好奇为什么它不会执行所有语句。
这里是定义:
#define THROW_AND_LOG(x, message) if(!x) { throw std::runtime_error(message); \
LOG::v1::MessageSender messageSender("log"); \
LOGGING_ERROR(messageSender,message);}
最佳答案
宏展开后,
if(!x)
转换为
if(!Foo::Initialize() && Bar::Initialize() && FooBar::Initialize())
Foo::Initialize()
可能返回 true
,因此 !Foo::Initialize()
为 false
和其他术语不执行(更不用说它们的含义已更改为相反)。
关于c++ - #define 不执行所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29108422/