mirror of
https://github.com/fmtlib/fmt.git
synced 2025-06-25 01:11:40 +02:00
Add support for incomplete types
This commit is contained in:
committed by
Victor Zverovich
parent
db405954cd
commit
c709138359
@ -1033,6 +1033,11 @@ enum {
|
||||
|
||||
struct view {};
|
||||
|
||||
template <typename T, typename Enable = std::true_type>
|
||||
struct is_view : std::false_type {};
|
||||
template <typename T>
|
||||
struct is_view<T, bool_constant<sizeof(T) != 0>> : std::is_base_of<view, T> {};
|
||||
|
||||
template <typename Char, typename T> struct named_arg;
|
||||
template <typename T> struct is_named_arg : std::false_type {};
|
||||
template <typename T> struct is_static_named_arg : std::false_type {};
|
||||
@ -2726,7 +2731,7 @@ template <typename... T> struct fstring {
|
||||
template <size_t N>
|
||||
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const char (&s)[N]) : str(s, N - 1) {
|
||||
using namespace detail;
|
||||
static_assert(count<(std::is_base_of<view, remove_reference_t<T>>::value &&
|
||||
static_assert(count<(is_view<remove_cvref_t<T>>::value &&
|
||||
std::is_reference<T>::value)...>() == 0,
|
||||
"passing views as lvalues is disallowed");
|
||||
if (FMT_USE_CONSTEVAL) parse_format_string<char>(s, checker(s, arg_pack()));
|
||||
|
@ -2540,3 +2540,29 @@ TEST(base_test, format_byte) {
|
||||
EXPECT_EQ(s, "42");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Only defined after the test case.
|
||||
struct incomplete_type;
|
||||
extern const incomplete_type& external_instance;
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
template <> struct formatter<incomplete_type> : formatter<int> {
|
||||
auto format(const incomplete_type& x, context& ctx) const -> appender;
|
||||
};
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
TEST(incomplete_type_test, format) {
|
||||
EXPECT_EQ(fmt::format("{}", external_instance), "42");
|
||||
}
|
||||
|
||||
struct incomplete_type {
|
||||
int i;
|
||||
};
|
||||
|
||||
const incomplete_type& external_instance = {42};
|
||||
|
||||
auto fmt::formatter<incomplete_type>::format(const incomplete_type& x,
|
||||
fmt::context& ctx) const
|
||||
-> fmt::appender {
|
||||
return formatter<int>::format(x.i, ctx);
|
||||
}
|
||||
|
Reference in New Issue
Block a user