Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::(customization point) invoke the most appropriate overload?

Since C++20, the concept of customization point is introduced in [namespace.std]/7:

Other than in namespace std or in a namespace within namespace std, a program may provide an overload for any library function template designated as a customization point, provided that (a) the overload's declaration depends on at least one user-defined type and (b) the overload meets the standard library requirements for the customization point. [ Note: This permits a (qualified or unqualified) call to the customization point to invoke the most appropriate overload for the given arguments. — end note ]

Does the note part (note the emphasized word "qualified") mean that std::f will automatically invoke the most appropriate overload for f if std::f is a customization point?

A real example is std::swap, which is a designated customization point. Does this mean since C++20, we can write std::swap(a, b) directly instead of using std::swap; swap(a, b);?

like image 731
xskxzr Avatar asked Oct 15 '22 05:10

xskxzr


1 Answers

A real example is std::swap, which is a designated customization point. Does this mean since C++20, we can write std::swap(a, b) directly instead of using std::swap; swap(a, b);?

No. std::swap itself did not gain any powers. It's still just a function template, so if you call it directly, you're... calling it directly. No ADL or anything.

The point of this is to say how customization points should be opted into. That is, you write:

namespace N { // not std
    void swap(Foo&, Foo&);
}

Not:

namespace std {
    void swap(N::Foo&, N::Foo&);
}

Nor:

namespace std {
    template <>
    void swap(N::Foo&, N::Foo&);
}

However, C++20 does introduce a lot of new things called customization point objects which you can use directly do this kind of thing. The CPO for swap is spelled std::ranges::swap (and likewise there are CPOs for all the useful ranges things... ranges::begin, ranges::end, etc.).

like image 146
Barry Avatar answered Oct 30 '22 20:10

Barry