Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return conditional `range_expression`

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.

like image 874
Tom Avatar asked Aug 27 '19 08:08

Tom


1 Answers

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.

like image 64
Piotr Skotnicki Avatar answered Sep 18 '22 05:09

Piotr Skotnicki