☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

除转义字符、宏定义之外不应使用反斜杠

3.7.3 ID_badBackslash
目录 › next › previous

反斜杠可用于标识转义字符,也可用于实现“伪换行”,即代码换行显示但在语法上并没有换行,一般用于宏定义,除此之外不应再使用反斜杠,否则没有实际意义,也会造成混乱。

示例:

#define M(x, y) if (x) {\   // Compliant
    ++(y);\                 // Compliant
}

void foo() {
    if (condition1 \        // Non-compliant, meaningless
     || condition2) {
    }
}

int a\                      // Non-compliant, odd usage
b\
c = 123;

void bar() {
    // comment  \           // Non-compliant, the next line is also commented out
    do_something();
}

如果“universal character name”被反斜杠截断会导致标准未定义的行为,如:

const char* s = "\u4e\      // Non-compliant, undefined behavior
2d";

应去掉反斜杠:

const char* s = "\u4e2d";   // Compliant

相关

ID_commentEndingBackslash

依据

ISO/IEC 14882:2003 2.1(1)-undefined ISO/IEC 14882:2011 2.2(1)-undefined
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.