Skip to content

Commit

Permalink
Workaround for libc++ incompatibility with C++ standard
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <[email protected]>
  • Loading branch information
phprus committed Aug 5, 2023
1 parent bdbbee2 commit c456d05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ struct formatter<BitRef, Char,
}
};

#ifdef _LIBCPP_VERSION

// Workaround for libc++ incompatibility with C++ standard.
// According to the Standard, `bitset::operator[] const` returns bool.
FMT_EXPORT
template <typename C, typename Char>
struct formatter<std::__bit_const_reference<C>, Char> : formatter<bool, Char> {
template <typename FormatContext>
FMT_CONSTEXPR20 auto format(const std::__bit_const_reference<C>& v,
FormatContext& ctx) const -> decltype(ctx.out()) {
return formatter<bool, Char>::format(static_cast<bool>(v), ctx);
}
};

#endif

FMT_END_NAMESPACE

#endif // FMT_STD_H_
7 changes: 7 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,10 @@ TEST(std_test, format_bit_reference) {
std::vector<bool> v = {true, false};
EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
}

TEST(std_test, format_const_bit_reference) {
const std::bitset<2> bs(1);
EXPECT_EQ(fmt::format("{} {}", bs[0], bs[1]), "true false");
const std::vector<bool> v = {true, false};
EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
}

0 comments on commit c456d05

Please sign in to comment.