This guards against hostile overloads of operator&. Thanks to Peter Dimov
for the report.
Details
- Reviewers
• Quuxplusone Mordante - Group Reviewers
Restricted Project - Commits
- rGe24ddb6027b6: [libc++] Use std::addressof in std::function::target
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/addressof.pass.cpp | ||
---|---|---|
21 | template<class T> friend void operator&(T) {} to expand its power a bit further. struct Incomplete; template<class T> struct Holder { T t; }; template<class T> struct Callable { int operator()() const { return 1; } }; int main(int, char**) { std::function<int()> f = Callable<Holder<Incomplete>>(); assert(f() == 1); return 0; } |
LGTM except for a minor issue.
libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/addressof.pass.cpp | ||
---|---|---|
23 | Can you use test/support/operator_hijacker.h's operator_hijacker instead? If not I would like the operator&() to be deleted. |
I don't see the point of having two ADL-proofing tests (robust_against_adl.pass.cpp and the new one), and I wonder why the old robust_against_adl.pass.cpp bothered to test three different cases but you're adding only one new case (like, should there be a matching test for std::function<int()> as well as std::function<void()>?) but anyway this certainly seems code-correct and sufficiently-tested at this point. :)
The added test case in addressof.pass.cpp is not checking for ADL proofing, but really for the specific case of using operator& instead of addressof. For example, the ADL proofing test would pass if we used __f_.__target().operator&(), but the operator hijacking one would not. Since we can't reasonably use .operator&() (other stuff would fail), this is mostly academic, but I think it still makes sense to have a test whose *intent* is specifically to test the reported bug.
template<class T> friend void operator&(T) {} to expand its power a bit further.
Or actually, even better, go all out with the traditional ADL-proofing test: