...
This compliant solution demonstrates a fully-portable, but likely inefficient, implementation of in_range() which compares test against each possible address in the range [r, n]. A compliant solution that is both efficient and fully-portable is currently unknown.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <iostream>
template <typename Ty>
bool in_range(const Ty *test, const Ty *r, size_t n) {
auto *cur = reinterpret_cast<const unsigned char *>(r);
auto *end = reinterpret_cast<const unsigned char *>(r + n);
auto *testPtr = reinterpret_cast<const unsigned char *>(test);
for (; cur != end; ++cur) {
if (cur == testPtr) {
return true;
}
}
return false;
}
void f() {
double foo[10];
double *x = &foo[0];
double bar;
std::cout << std::boolalpha << in_range(&bar, x, 10);
}
|
...