Skip to content

Commit

Permalink
fix format ambiguity if implicitly convertible to std::tuple (#33)
Browse files Browse the repository at this point in the history
Change-Id: I5fdc83325dc2e66a5100431c92fa75017dbadc7b
  • Loading branch information
oliverlee authored Aug 29, 2024
1 parent 0976682 commit 306be7a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
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

0 comments on commit 306be7a

Please sign in to comment.