Skip to content

Commit c709138

Browse files
localspookvitaut
authored andcommitted
Add support for incomplete types
1 parent db40595 commit c709138

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

include/fmt/base.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,11 @@ enum {
10331033

10341034
struct view {};
10351035

1036+
template <typename T, typename Enable = std::true_type>
1037+
struct is_view : std::false_type {};
1038+
template <typename T>
1039+
struct is_view<T, bool_constant<sizeof(T) != 0>> : std::is_base_of<view, T> {};
1040+
10361041
template <typename Char, typename T> struct named_arg;
10371042
template <typename T> struct is_named_arg : std::false_type {};
10381043
template <typename T> struct is_static_named_arg : std::false_type {};
@@ -2726,7 +2731,7 @@ template <typename... T> struct fstring {
27262731
template <size_t N>
27272732
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const char (&s)[N]) : str(s, N - 1) {
27282733
using namespace detail;
2729-
static_assert(count<(std::is_base_of<view, remove_reference_t<T>>::value &&
2734+
static_assert(count<(is_view<remove_cvref_t<T>>::value &&
27302735
std::is_reference<T>::value)...>() == 0,
27312736
"passing views as lvalues is disallowed");
27322737
if (FMT_USE_CONSTEVAL) parse_format_string<char>(s, checker(s, arg_pack()));

test/format-test.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,3 +2540,29 @@ TEST(base_test, format_byte) {
25402540
EXPECT_EQ(s, "42");
25412541
}
25422542
#endif
2543+
2544+
// Only defined after the test case.
2545+
struct incomplete_type;
2546+
extern const incomplete_type& external_instance;
2547+
2548+
FMT_BEGIN_NAMESPACE
2549+
template <> struct formatter<incomplete_type> : formatter<int> {
2550+
auto format(const incomplete_type& x, context& ctx) const -> appender;
2551+
};
2552+
FMT_END_NAMESPACE
2553+
2554+
TEST(incomplete_type_test, format) {
2555+
EXPECT_EQ(fmt::format("{}", external_instance), "42");
2556+
}
2557+
2558+
struct incomplete_type {
2559+
int i;
2560+
};
2561+
2562+
const incomplete_type& external_instance = {42};
2563+
2564+
auto fmt::formatter<incomplete_type>::format(const incomplete_type& x,
2565+
fmt::context& ctx) const
2566+
-> fmt::appender {
2567+
return formatter<int>::format(x.i, ctx);
2568+
}

0 commit comments

Comments
 (0)