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

boost::hana::drop_front doesn't respect refness #508

Open
KierenP opened this issue Aug 17, 2022 · 1 comment
Open

boost::hana::drop_front doesn't respect refness #508

KierenP opened this issue Aug 17, 2022 · 1 comment

Comments

@KierenP
Copy link

KierenP commented Aug 17, 2022

#include <boost/hana/ext/std/tuple.hpp>
#include <boost/hana/fold_left.hpp>
#include <boost/hana/find_if.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/transform.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>

#include <iostream>
#include <tuple>
#include <vector>

namespace hana = boost::hana;

int main()
{
    int x = 1;
    int y = 2;
    int z = 3;

    std::tuple<int&, int&, int&> refs {x, y, z};
    auto ref_drop = boost::hana::drop_front(refs);

    x = 10;
    y = 20;
    z = 30;

    std::cout << x << " " << y << " " << z << std::endl;
    std::cout << std::get<0>(refs) << " " << std::get<1>(refs) << " " << std::get<2>(refs) << std::endl;
    std::cout << std::get<0>(ref_drop) << " " << std::get<1>(ref_drop) << std::endl;
}

The output of the above code is:

10 20 30
10 20 30
2 3

Expected output is:

10 20 30
10 20 30
20 30

I would expect if I pass boost::hana::drop_front a std::tuple<T&, U&, V&> to get back a std::tuple<U&, V&> but instead I get a std::tuple<U, V>. This is unexpected. Is this intended behavior?

@KierenP
Copy link
Author

KierenP commented Aug 17, 2022

As an aside, what is the best way to iterate over tuple elements until a condition is true? For example if I have a std::tuple (std::tuple<X, Y, Z>) and I want to call .fn(...) on each element I can do it like this:

    boost::hana::for_each(my_tuple, [](auto&& x) 
    { 
        x.fn();
    });

But if I want to iterate only until a precondition is hit, I can't figure out how to do this

    auto first_ok_result = boost::hana::for_each(my_tuple, [](auto&& x) 
    { 
        auto result = x.fn();
        if (result.status == status::ok)
            return result.value; // I want to stop the for_each and return this value
    });

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