2015-06-19 16 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 derlenmiyor. Hem clang ++ (3.5.2/cygwin) hem de Visual Studio (2013) belirsiz çağrıdan şikayet ediyor. using yönergesi kullanılmadığından, neyin yanlış olduğunu anlamıyorum. Başarıyla derlemek için :: öneki yardımcı olur.clang ++: error: 'bölüm' çağrısı belirsiz

cevap

8

Kişisel partition o std:: ad taşıyan std::vector<int>::iterator olan argümanlar, üzerinde argument dependent lookup (ADL) kullandığından std:: öneki bile olmadan, std::partition

bunu yapıyor nedeni ile bir isim çarpışma vardır. Bu nedenle, derleyici std::partition işlevinin yanı sıra partition işlevinizi "görebiliyor".

From cppreference (vurgu benim)

... 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