1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <iterator>
namespace utility
{
template< typename INPUT_ITERATOR, typename OUTPUT_ITERATOR, typename PREDICATE >
void copy_if( INPUT_ITERATOR begin, INPUT_ITERATOR end, OUTPUT_ITERATOR dest, PREDICATE fn )
{
for( ; begin != end ; ++begin ) if( fn(*begin) ) *dest++ = *begin ;
}
}
using namespace utility ; // *** trouble
int main()
{
std::vector<int> all_numbers { 1, 2, 3, 4, 5, 6, 7 } ;
std::vector<int> odd_numbers ;
// without the using directive, the code would have been this and things would be fine with C++11
utility::copy_if( all_numbers.begin(), all_numbers.end(), std::back_inserter(odd_numbers),
std::bind2nd( std::modulus<int>(), 2 ) ) ;
// with the using directive, the code was this. *** error ***: call to 'copy_if' is ambiguous in C++11
// see Koenig lookup (ADL): https://en.wikipedia.org/wiki/Argument-dependent_name_lookup
copy_if( all_numbers.begin(), all_numbers.end(), std::back_inserter(odd_numbers),
std::bind2nd( std::modulus<int>(), 2 ) ) ;
}
|