Skip to content

Commit

Permalink
Add an is_formattable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 28, 2021
1 parent 5788740 commit 835b910
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,11 @@ using wformat_context = buffer_context<wchar_t>;
#define FMT_BUFFER_CONTEXT(Char) \
basic_format_context<detail::buffer_appender<Char>, Char>

template <typename T, typename Char = char>
using is_formattable = bool_constant<!std::is_same<
decltype(detail::arg_mapper<buffer_context<Char>>().map(std::declval<T>())),
detail::unformattable>::value>;

/**
\rst
An array of references to arguments. It can be implicitly converted into
Expand Down
6 changes: 6 additions & 0 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ TEST(CoreTest, HasFormatter) {
"");
}

TEST(CoreTest, IsFormattable) {
static_assert(fmt::is_formattable<enabled_formatter>::value, "");
static_assert(!fmt::is_formattable<disabled_formatter>::value, "");
static_assert(fmt::is_formattable<disabled_formatter_convertible>::value, "");
}

struct convertible_to_int {
operator int() const { return 42; }
};
Expand Down

0 comments on commit 835b910

Please sign in to comment.