【问题标题】:In Visual Studio, how can I detect all the occurrences in C++ code of the ternary operator c?e1:e2 where e1 and e2 are not of the same type?在 Visual Studio 中,如何检测 C++ 代码中所有出现的三元运算符 c?e1:e2 其中 e1 和 e2 不是同一类型?
【发布时间】:2021-09-03 05:35:40
【问题描述】:

在 Visual Studio 中,如何检测 C++ 代码中三元运算符 c?e1:e2 的所有出现,其中 e1e2 不是同一类型?

我对检测c?1:2 不感兴趣,例如我对c?0:std::string{"Hello world"} 感兴趣。

【问题讨论】:

  • 我认为你不能让它比标准更严格。
  • @molbdnilo 我很高兴能在我的代码中找到所有这些 :-) find 就像在 Find in Files 命令中一样。跨度>
  • 由于标准允许从一个表达式的类型到另一个表达式的类型的明确隐式转换,你可能不走运。

标签: c++ visual-studio code-analysis conditional-operator static-code-analysis


【解决方案1】:

如果您的代码使用 Visual Studio 编译,您可以使用:/Zc:ternary

此选项启用标准符合行为,其中类型不能不相关。

https://docs.microsoft.com/en-us/cpp/build/reference/zc-ternary?view=msvc-160

【讨论】:

  • 不幸的是,0 可以明确地转换为 std::string(并且很可能会导致运行时错误),因此无法检测到该示例。
  • @gerum 在我看来,c?0:std::string{"Hello world"} 适合我的 Visual Studio 编译器,无论是 /Zc:ternary 还是 /Zc:ternary-
  • @molbdnilo:有趣的是,0 在运行时可模棱两可地转换为 std::string,但结果未定义。哦,快乐!
  • 0std::string 的转换是什么? CharT* ctor 和 0 充当 nullptr?
  • @NathanPierson 是的,0nullptr 都是空指针常量
猜你喜欢
  • 2012-12-22
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多