可以省略 std::
<algorithm>
的命名空间s 当参数类型在该 namespace 中时,通常是这种情况。是否有任何警告或 clang-tidy 规则可以发现此类遗漏?
#include <vector>
#include <algorithm>
std::vector<int> v;
for_each(v.begin(), v.end(), [](auto){});
return 0;
上面的示例,使用最新的 clang 和 -Wall、-Wextra 和 -Wpedantic 编译,不会发出任何诊断信息:
最佳答案
tidy 中有一个开放的变化可以用来标记这个:
[patch] Summary
This patch adds
bugprone-unintended-adl
which flags uses of ADL that are not on the provided whitelist.bugprone-unintended-adl
Finds usages of ADL (argument-dependent lookup), or potential ADL in the case of templates, that are not on the provided lists of allowed identifiers and namespaces. [...]
.. option::
IgnoreOverloadedOperators
If non-zero, ignores calls to overloaded operators using operator syntax (e.g.
a + b
), but not function call syntax (e.g.operator+(a, b)
). Default is1
... option::
AllowedIdentifiers
Semicolon-separated list of names that the check ignores. Default is
swap;make_error_code;make_error_condition;data;begin;end;rbegin;rend;crbegin;crend;size;ssize;empty
... option::
AllowedNamespaces
Semicolon-separated list of namespace names (e.g.
foo;bar::baz
). If the check finds an unqualified call that resolves to a function in a namespace in this list, the call is ignored. Default is an empty list.
不过,自 2020 年 7 月以来,该补丁似乎没有任何事件,但如果 OP 对此感兴趣,则 OP 可以尝试恢复该补丁。
关于c++ - 警告由于 ADL 而丢失 std::prefixes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70261360/