2015-06-19 22 views
7
#include <algorithm> 
#include <vector> 

template <class BidirectionalIterator, class UnaryPredicate> 
BidirectionalIterator partition(BidirectionalIterator first, 
    BidirectionalIterator last, UnaryPredicate pred) 
{ 
    while (first != last) { 
     while (pred(*first)) { 
      ++first; 
      if (first == last) return first; 
     } 
     do { 
      --last; 
      if (first == last) return first; 
     } while (!pred(*last)); 
     std::swap(*first, *last); 
     ++first; 
    } 
    return first; 
} 

int main() { 
    std::vector<int> v = { 1, 55, 17, 65, 40, 18, 77, 37, 77, 37 }; 
    partition(v.begin(), v.end(), [](const int &i) { 
     return i < 40; 
    }); 
    return 0; 
} 

Kod nie zostanie skompilowany. Zarówno clang ++ (3.5.2/cygwin), jak i Visual Studio (2013) narzekają na niejednoznaczne wywołanie. Ponieważ nie zastosowano dyrektywy using, nie rozumiem, co jest nie tak. Aby przeprowadzić kompilację, należy użyć prefiksu ::.clang ++: błąd: wywołanie 'partition' jest niejednoznaczne.

Odpowiedz

8

Twój partition ma kolizji nazw z std::partition

przyczyną tego, że robi tak, nawet bez przedrostka std:: dlatego, że używa argument dependent lookup (ADL) na argumentach, które są std::vector<int>::iterator, które niosą nazw std::. Dlatego kompilator jest w stanie "zobaczyć" funkcję std::partition, a także funkcję partition.

From cppreference (kopalni nacisk)

... for every argument in a function call expression and for every template argument of a template function, its type is examined to determine the associated set of namespaces and classes that it will add to the lookup

Powiązane problemy