Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with empty container(s)? #3

Open
misuo opened this issue Nov 9, 2016 · 0 comments
Open

Working with empty container(s)? #3

misuo opened this issue Nov 9, 2016 · 0 comments

Comments

@misuo
Copy link

misuo commented Nov 9, 2016

First, let me thank you the for this. Seems exactly what I'm looking for.

Have you considered what happens/should-happen if one of the containers are empty?

I get a crash when trying to work with that, e.g. as illustrated:

#include "product_iterator.hpp"
#include <iostream>
#include <vector>

using namespace std;

int main()
{
  vector<int> c1({1,2});
  vector<char> c2;         // NOTE THIS IS EMPTY!

  auto it = make_product_iterator(c1, c2);
  auto end = it.get_end();
  for(; it != end; ++it)
  {
    cout << "{" << it.get<0>() << ", " << it.get<1>() << "}\n";
  }
}

To allow client to use/test for that you could e.g. introduce a new method - similar to the get<>() - that tests for whether the container iterator is at its end, e.g. something like:

template <size_t I>
bool not_end() {
    return std::get<I>(current_)!=std::get<I>(end_);
}

I've tried to add that (se snippet code below for usage), but it still crashes al beit after output the first expected combination. I haven't examined (yet) what is causing the crash, but likely it is related to how you advance iterators in your implementation.

  for(; it != end; ++it)
  {
    cout << "{" << it.get<0>();
    if( it.not_end<1>() ) cout << ", " << it.get<1>();
    cout << "}\n";
 }

Expected output:

{1}
{2}

Regards,
/Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant