Code #1
#include <iterator>
#include <algorithm>
#include <iostream>
#include <vector>
template <typename container>
void sort(typename container::iterator beginning,
typename container::iterator end)
{
std::cout << "calling custom sorting function\n";
}
int main()
{
std::vector<int> v{1, 2, 3};
sort(v.begin(), v.end());
}
Code description
It is gonna call the std::sort
function, which is found by ADL. Though the code below:
Code #2
#include <iterator>
#include <algorithm>
#include <iostream>
#include <vector>
template <typename Iterator>
void sort(Iterator beginning,
Iterator end)
{
std::cout << "calling custom sorting function\n";
}
int main()
{
std::vector<int> v{1, 2, 3};
sort(v.begin(), v.end());
}
Causes ambiguous overload error. So I have two questions:
Questions
How
container
was deduced in code #1?From what I know, type deduction during template instantiation cannot backtrack through member types to find the enclosing one (
std::vector<int>
in this case).Even if it could backtrack, why did it compile without causing ambiguous overload error?
container
is not deduced. This is SFINAE. wandbox.org/permlink/OGnCoxCqWapQeRxG – Bridgettbridgette