c++ - 警告由于 ADL 而丢失 std::prefixes

标签 c++ compiler-warnings argument-dependent-lookup clang-tidy

可以省略 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 编译,不会发出任何诊断信息:

https://godbolt.org/z/dTsKbbEKe

最佳答案

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 is 1.

.. 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/

相关文章:

c++ - vxWorks-Workbench 6.8 未解析的 C++ 符号

未分配 C++ vector 值

c++ - 如何解决警告 C4333 ('>>' : right shift by too large amount, 数据丢失)

c++ - 具有相同参数的命名空间中的函数和函数之间的歧义

c++ - 为什么我不能在另一个函数中定义一个函数?

c# - 为什么连续抛出2个异常不会产生无法访问的代码警告?

c++ - 禁用局部变量的未初始化警告

c++ - 通过模板类的基类进行参数相关查找

c++ - 没有匹配的函数用于调用 std::list::remove_if( function()::predicate )

c++ - 当数组从函数返回时,为什么我不能使用基于范围的循环?