如何使用继续或中断表达式编写多行宏
How to write multiline macro with continue or break expression
在我们最近的项目中,我编写了一个宏,该宏将在给定条件下继续,并且如此处所述 C 多行宏:do/while(0( vs 作用域块 我试图使用 do--while 来实现这一点。下面是示例代码来说明这一点:
#define PRINT_ERROR_AND_CONTINUE_IF_EMPTY(dummy,dummystream,symbol,errorString)
do{
if(dummy.empty())
{
dummyStream<<symbol<<",Failed,"<<errorString<<std::endl;
continue;
}
}while(0)
int main()
{
int x =9;
std::ofstream& ofsReportFile;
while(x>5)
{
std::string str;
PRINT_ERROR_AND_CONTINUE_IF_EMPTY(str,ofsReportFile,"Test","Test String is Empty");
std::cout<<x<<std::endl;
x--;
}
return 0;
}
但是,这并没有按预期工作,原因可能是在里面继续语句,所以质疑如何使用 continue 语句编写多行宏,并且该宏的用户可以像 CONTINUE_IF_EMPTY(str(;
lambda怎么样?这看起来像是该宏的一个很好的替代品,该宏既是全局的(就像所有宏一样(,又是奇怪的特定。此外,您还可以获得捕获以帮助减少重复的参数。
int main()
{
int x = 9;
std::ofstream& ofsReportFile = /* ... */;
auto const reportEmpty = [&](std::string const &str) {
if(str.empty()) {
ofsReportFile << "Test, Failed, Test String is Empty" << std::endl;
return true;
}
return false;
};
while(x > 5)
{
std::string str;
if(reportEmpty(str))
continue;
std::cout << x << std::endl;
x--;
}
}
相关文章:
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 在 lambda 表达式中使用 std::atomic
- 组合正则表达式部分使用 | 不起作用的 C++
- 整体模板参数。错误:在常量表达式中使用'this'
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- 表达式中使用的枚举器是否与其枚举的基础类型具有相同的类型?
- 在折叠表达式中使用 lambda 时"Uninitialized captured reference"错误 - clang vs gcc
- 为什么我不能在我的三元表达式中使用 return,但我可以在常规的 if-else 语句中使用?
- 使用从不打算在常量表达式中使用的 constexpr 变量有什么好处吗?
- 更改对象并将其在同一表达式中使用它是不确定的行为,但是子表达式由逗号运算符分开
- 如何在条件表达式中使用 'cin' 正确退出 while 循环?(C++,CLion)
- 您可以在表达式中使用 void 返回函数吗?
- C 使用整数表达式中使用和操作员
- 无限数组C++在一个表达式中使用两个新值调整数组大小
- 静态之前的预期主要表达式?必须使用常量指针
- “无效的操作数是二进制表达式”当使用自定义结构作为C 中的MAP索引时
- 在 lambda 表达式中使用外部函数
- 如何在C++的正则表达式中使用变量
- 如何初始化类中的数组,该类的值可以在常量表达式中使用
- 如何在 C/C++ PCRE 正则表达式中使用 UTF-8 文字字符