zcl*_*lll 2 c++ overloading sfinae overload-resolution argument-dependent-lookup
考虑以下代码:
#include<vector>
#include<ranges>
#include<algorithm>
//using namespace std;
using namespace std::ranges;
int main()
{
std::vector<int> a = {};
sort(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它运行正常。
显然,它调用了这个重载函数(函子,严格来说):
template<random_access_range _Range,
typename _Comp = ranges::less, typename _Proj = identity>
requires sortable<iterator_t<_Range>, _Comp, _Proj>
constexpr borrowed_iterator_t<_Range>
operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__comp), std::move(__proj));
}
Run Code Online (Sandbox Code Playgroud)
但是在我们引入命名空间 std 后,函数调用变得不明确(出现编译错误):
#include<vector>
#include<ranges>
#include<algorithm>
using namespace std;
using namespace std::ranges;
int main()
{
std::vector<int> a = {};
sort(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
除了之前的重装之外
2045 | inline constexpr __sort_fn sort{};
Run Code Online (Sandbox Code Playgroud)
,在命名空间 std 中还发现了许多其他重载函数,例如:
template<class _ExecutionPolicy, class _RandomAccessIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator)
Run Code Online (Sandbox Code Playgroud)
和
template<class _RAIter, class _Compare> constexpr void std::sort(_RAIter, _RAIter, _Compare)
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
sort(a),那么根据 的 ADL,它在第一个代码中不应该同样可见吗a?| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |