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

Add convenience operator<< functions for stl containers #331

Open
Snaipe opened this issue Jan 4, 2020 · 0 comments
Open

Add convenience operator<< functions for stl containers #331

Snaipe opened this issue Jan 4, 2020 · 0 comments
Labels
Milestone

Comments

@Snaipe
Copy link
Owner

@Snaipe Snaipe commented Jan 4, 2020

When I fixed the warnings & compilation errors with the zero() criterion, I realized that I couldn't compile the following:

using int_vect = std::vector<int>;
cr_expect(not(zero(type(int_vect), int_vect{})));

It became apparent quickly that the reason why this doesn't work is that the STL doesn't define operator<< on container types. Worse, I couldn't just define that operator in the top-level namespace, because overload resolution would fail to pick it up.

The workaround is to wrap the container type in a "printable" type:

struct int_vect {
    std::vector<int> v{};
};

std::ostream &operator<<(std::ostream &out, const int_vect &v)
{
    out << "std::vector<> {";
    if (v.v.size() > 0) {
        out << v.v[0];
    }
    for (size_t i = 1; i < v.v.size(); i++) {
        out << ", " << v.v[i];
    }
    out << "}";
    return out;
}

bool operator==(const int_vect &v1, const int_vect &v2)
{
    return v1.v == v2.v;
}

This should obviously be working out of the box though. I think criterion should define sane operator<< defaults for STL containers in the criterion::internal::stream_override namespace, just like we currently do for std::string and std::wstring, so that these get picked up by default.

@Snaipe Snaipe added API Bug labels Jan 4, 2020
@Snaipe Snaipe added this to the v2.4.0 milestone Jan 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.