Skip to content

Commit dd52f79

Browse files
committed
Merge bitcoin/bitcoin#24416: doc: Avoid ADL for function calls
52a797b doc: Avoid ADL for function calls (Hennadii Stepanov) Pull request description: It happened two times recently, when [ADL](https://en.cppreference.com/w/cpp/language/adl) popped up unexpectedly and brought some confusion: - https://github.com/bitcoin/bitcoin/pull/24338/files#r805989994 > Any idea why this even compiles? - https://www.erisian.com.au/bitcoin-core-dev/log-2022-02-18.html#l-51: > 2022-02-18T03:24:14 \<dongcarl\> Does anyone know why this compiles? dongcarl/bitcoin@6d3d2ca > 2022-02-18T03:24:14 \<dongcarl\> GetUTXOStatsWithHasher and MakeUTXOHasher are both in the `kernel::` namespace and I never added a `using` declaration on top... > 2022-02-18T03:25:53 \<sipa\> https://en.cppreference.com/w/cpp/language/adl ? Let's document our intention to avoid similar cases in the future. ACKs for top commit: laanwj: Anyhow, ACK 52a797b, there is no need to hold merge up on this, documenting it is a step forward. Tree-SHA512: f52688b5d8f6130302185206ec6ea4731b099a75294ea2d477901a52d6d58473e3427e658aea408c140c2824c37a0399ec7376aded2a91197895ea52d51f0018
2 parents e157b98 + 52a797b commit dd52f79

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/developer-notes.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ code.
110110
- `nullptr` is preferred over `NULL` or `(void*)0`.
111111
- `static_assert` is preferred over `assert` where possible. Generally; compile-time checking is preferred over run-time checking.
112112

113+
For function calls a namespace should be specified explicitly, unless such functions have been declared within it.
114+
Otherwise, [argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl), also known as ADL, could be
115+
triggered that makes code harder to maintain and reason about:
116+
```c++
117+
#include <filesystem>
118+
119+
namespace fs {
120+
class path : public std::filesystem::path
121+
{
122+
};
123+
// The intention is to disallow this function.
124+
bool exists(const fs::path& p) = delete;
125+
} // namespace fs
126+
127+
int main()
128+
{
129+
//fs::path p; // error
130+
std::filesystem::path p; // compiled
131+
exists(p); // ADL being used for unqualified name lookup
132+
}
133+
```
134+
113135
Block style example:
114136
```c++
115137
int g_count = 0;

0 commit comments

Comments
 (0)