What's the most efficient way of iterating over one of several known ranges based on some condition?
pseudo-code for a binary condition:
for element in (condition ? range_a : range_b)
// do work
This 'example' shows my intention using a range-based for loop but as std::initializer_list
has reference semantics it won't work.
constexpr auto some_range(bool c) -> std::initializer_list<int> {
if (c) {
return {1,2};
} else {
return {3, 4, 5};
}
}
bool cond = true; // false
for(auto x : some_range(cond)) {
// things
}
yields: warning: returning address of local temporary object [-Wreturn-stack-address]
During run-time I could return a std::vector
but that would involve constructing a new vector every call:
auto some_range(bool c) -> std::vector<int> {
if (c) {
return {1,2};
} else {
return {3, 4, 5};
}
}
I could use a fixed size std::array
of std::optional<int>
but I have to resort to a C++14 or c++11 solution.
A range-based for loop can iterate over any expression e
, whose class type has e.begin()
and e.end()
member functions, or non-member functions begin(e)
and end(e)
can be found via ADL. A simple iterable view can be therefore:
#include <cstddef>
template <typename T>
struct view
{
T* p;
std::size_t s;
constexpr T* begin() const { return p; }
constexpr T* end() const { return p + s; }
};
and then returned holding a pointer to an array with, e.g., static storage duration:
inline view<const int> conditional_range(bool a)
{
static int ra[] = { 1, 2 };
static int rb[] = { 3, 4, 5 };
if (a) return { ra, 2 };
else return { rb, 3 };
}
DEMO
This is similar to what c++20 offers with std::span
.
An std::initilizer_list<T>
wraps a local array, constructed automatically from the brace-enclosed initializer, and so is not usable as a return type, because in such a case the pointers it stores are invalidated on function exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With