cppitertools icon indicating copy to clipboard operation
cppitertools copied to clipboard

[c++17] replace boost optional with std::optional in zip_longest

Open ryanhaining opened this issue 8 years ago • 7 comments

  • update examples/zip_longest_examples.cpp to use structured bindings
  • update README example to use structured bindings

ryanhaining avatar Feb 18 '17 00:02 ryanhaining

std::optional doesn't support reference types. I can use a std::optional<std::reference_wrapper<T>> but that'll throw off uses with assignment

ryanhaining avatar Feb 23 '17 00:02 ryanhaining

Is it possible to replace boost::optional with C Pointers in zip_longest?

xjjgjmeng avatar Jul 07 '23 09:07 xjjgjmeng

I can't imagine how, do you have something in mind?

ryanhaining avatar Jul 07 '23 17:07 ryanhaining

#include <zip_longest.hpp>

#include <vector>
#include <string>
#include <iostream>

int main() {
    std::vector<int> ivec{1, 4, 9, 16, 25, 36};
    std::vector<std::string> svec{"hello", "good day", "goodbye"};

    // i: int*
    // s: std::string*
    for (auto&& [i, s] : iter::zip_longest(ivec, svec)) {
        if (i) {
            std::cout << *i << std::endl;
        }

        if (s) {
            std::cout << *s << std::endl;
        }
    }
}

xjjgjmeng avatar Jul 08 '23 01:07 xjjgjmeng

of course you could implement a zip_longest like that, but changes to the yielded type cannot break existing uses.

ryanhaining avatar Jul 08 '23 17:07 ryanhaining

Changing the type to a pointer doesn't change the existing usage, does it?

xjjgjmeng avatar Jul 10 '23 06:07 xjjgjmeng

I can't change the library in a way that breaks existing users if they update to a newer version of the library.

ryanhaining avatar Jul 10 '23 21:07 ryanhaining