static_pointer_cast resides in std namespace. Yet it takes pointers that are also in std namespace.
So both std:: qualified calls and unqualified calls are accepted.
Which one is the right, more idiomatic way to call static_pointer_cast?
static_pointer_cast resides in std namespace. Yet it takes pointers that are also in std namespace.
So both std:: qualified calls and unqualified calls are accepted.
Which one is the right, more idiomatic way to call static_pointer_cast?
Copyright © 2021 Jogjafile Inc.
Generally, if you know exactly which function you want to call, rather than allowing for customization, I would try to avoid ADL, implying avoiding unqualified calls.
In this case it is not expected for a library to provide its own
static_pointer_castforshared_ptrs of their own type. You always want to call thestdversion. Callingstd::static_pointer_castdirectly guarantees that no other suchstatic_pointer_castwill interfere.If you include e.g. a library that also declared some function named
static_pointer_castwhich happens to be viable for your call and is a better fit or more specialized in overload resolution than thestdone, you may accidentally call it instead if you use an unqualified call.I think this is extremely unlikely to be a problem in the case of
static_pointer_cast, because it is unlikely that a library will use that name in a way that the overload will be viable for a normalstd::static_pointer_castcall and be at the same time a better match, all of it without realizing the implications.But for example for
std::launderthis seems to have happened in some code bases (the other way around) when C++17 was introduced and thestdoverload turned out to be a better match than the library one in an unqualified call with a standard library type as argument.If you look at for example C++ standard library implementations you will see that they follow this rule to avoid unqualified calls very strictly and use them only where the standard expects the user to be able to customize the call. Of course for non-library code especially this might not be as important of a consideration.
But also note that prior to C++20, if there is no function template named
static_pointer_castfound by usual unqualified lookup, then an unqualified call using an explicit template argument list (which are required forstatic_pointer_cast) will fail anyway. So before C++20 you don't really have the choice to use the unqualified call without a priorusingdeclaration to import the function.