ryanhaining / cppitertools

Implementation of python itertools and builtin iteration functions for C++17
https://twitter.com/cppitertools
BSD 2-Clause "Simplified" License
1.37k stars 115 forks source link

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

Open ryanhaining opened 7 years ago

ryanhaining commented 7 years ago
ryanhaining commented 7 years ago

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

xjjgjmeng commented 1 year ago

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

ryanhaining commented 1 year ago

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

xjjgjmeng commented 1 year ago
#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;
        }
    }
}
ryanhaining commented 1 year ago

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

xjjgjmeng commented 1 year ago

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

ryanhaining commented 1 year ago

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