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

fix format ambiguity if implicitly convertible to std::tuple #33

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/detail/arg_fmt.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "src/detail/is_range.hpp"
#include "src/detail/is_specialization_of.hpp"
#include "src/detail/priority.hpp"
#include "src/detail/type_name.hpp"
#include "src/rope.hpp"
Expand Down Expand Up @@ -38,9 +39,16 @@ class arg_fmt_fn
}

template <
class... Ts,
std::enable_if_t<not is_ostreamable_v<std::tuple<Ts...>>, bool> = true>
static constexpr auto impl(priority<2>, const std::tuple<Ts...>& arg)
class T,
std::enable_if_t<
is_specialization_of_v<T, std::tuple> and not is_ostreamable_v<T>,
bool> = true>
static constexpr auto impl(priority<2>, const T& arg)
{
return tuple_impl(arg);
}
template <class... Ts>
static constexpr auto tuple_impl(const std::tuple<Ts...>& arg)
{
return tuple_fmt<Ts...>{arg};
}
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ skytest_test(
stdout = [
"empty(\\[1, 2, 3\\])",
"empty({4}{5})",
"custom_tuple != custom_tuple",
"wrapped{...} == wrapped{...}",
],
)
Expand Down
15 changes: 15 additions & 0 deletions test/arg_fmt_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ struct array : std::array<T, N>
}
};

template <class... Ts>
struct custom_tuple : std::tuple<Ts...>
{
friend auto& operator<<(std::ostream& os, const custom_tuple&)
{
os << "custom_tuple";
return os;
}
};

struct wrapped
{
int value;
Expand All @@ -39,6 +49,7 @@ auto main() -> int
using namespace ::skytest::literals;
using ::skytest::eq;
using ::skytest::expect;
using ::skytest::ne;

"array uses default range arg fmt"_test = [] {
return expect(empty(std::array{1, 2, 3}));
Expand All @@ -48,6 +59,10 @@ auto main() -> int
return expect(empty(array<int, 2>{4, 5}));
};

"custom tuple uses custom fmt"_test = [] {
return expect(ne(custom_tuple{}, custom_tuple{}));
};

"type without operator<< is displayed"_test = [] {
return expect(eq(wrapped{1}, wrapped{2}));
};
Expand Down
Loading