I am testing my implementation of binary search from binsearch.hpp:
template<typename Iterator, typename T>
Iterator binsearch(Iterator begin, Iterator end, const T &v) {
if (std::distance(begin, end) == 0) {return end;}
Iterator save = end;
while (std::distance(begin, end) > 0) {
Iterator mid = begin + std::distance(begin, end) / 2;
if (*mid == v) {
return mid;
}
if (v < *mid) {
end = mid;
} else {
begin = mid + 1;
}
}
return save;
}
with the following boost-driven unit test test_binsearch.cpp:
#define BOOST_TEST_MODULE test_binsearch
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "binsearch.hpp"
BOOST_AUTO_TEST_CASE(empty_0) {
std::vector<int> xs = {};
const auto result = binsearch(xs.begin(), xs.end(), 42);
BOOST_TEST((result == xs.end()));
}
Unless I surround the comparison inside the BOOST_TEST with an extra pair of parentheses, I get a very cryptic and long compilation error that repeatedly tries to convert an iterator to a char, an error code and a few other types:
note: cannot convert ‘t’ (type ‘const __gnu_cxx::__normal_iterator std::vector >’) to type ‘char’
ostr << t;
I got the clue to surround my comparison with extra parentheses from here. Why does it fail to compile without them?
Aucun commentaire:
Enregistrer un commentaire