Skip to content

Commit

Permalink
Protect against overloaded comma operators in decltype
Browse files Browse the repository at this point in the history
  • Loading branch information
morinmorin authored and vitaut committed Feb 6, 2019
1 parent 467520e commit 430e6ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ template <typename Char> struct test_stream : std::basic_ostream<Char> {
template <typename T, typename Char> class is_streamable {
private:
template <typename U>
static decltype(internal::declval<test_stream<Char>&>()
<< internal::declval<U>(),
static decltype((void)(internal::declval<test_stream<Char>&>()
<< internal::declval<U>()),
std::true_type())
test(int);

Expand Down
12 changes: 7 additions & 5 deletions include/fmt/prepare.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,15 @@ struct parts_container_concept_check : std::true_type {

template <typename T> static std::false_type has_add_check(check_second);
template <typename T>
static decltype(declval<T>().add(declval<typename T::format_part_type>()),
static decltype((void)declval<T>().add(
declval<typename T::format_part_type>()),
std::true_type()) has_add_check(check_first);
typedef decltype(has_add_check<PartsContainer>(check_first())) has_add;
static_assert(has_add::value, "PartsContainer doesn't provide add() method");

template <typename T> static std::false_type has_last_check(check_second);
template <typename T>
static decltype(declval<T>().last(),
static decltype((void)declval<T>().last(),
std::true_type()) has_last_check(check_first);
typedef decltype(has_last_check<PartsContainer>(check_first())) has_last;
static_assert(has_last::value,
Expand All @@ -554,7 +555,8 @@ struct parts_container_concept_check : std::true_type {
static std::false_type has_substitute_last_check(check_second);
template <typename T>
static decltype(
declval<T>().substitute_last(declval<typename T::format_part_type>()),
(void)declval<T>().substitute_last(
declval<typename T::format_part_type>()),
std::true_type()) has_substitute_last_check(check_first);
typedef decltype(has_substitute_last_check<PartsContainer>(
check_first())) has_substitute_last;
Expand All @@ -563,15 +565,15 @@ struct parts_container_concept_check : std::true_type {

template <typename T> static std::false_type has_begin_check(check_second);
template <typename T>
static decltype(declval<T>().begin(),
static decltype((void)declval<T>().begin(),
std::true_type()) has_begin_check(check_first);
typedef decltype(has_begin_check<PartsContainer>(check_first())) has_begin;
static_assert(has_begin::value,
"PartsContainer doesn't provide begin() method");

template <typename T> static std::false_type has_end_check(check_second);
template <typename T>
static decltype(declval<T>().end(),
static decltype((void)declval<T>().end(),
std::true_type()) has_end_check(check_first);
typedef decltype(has_end_check<PartsContainer>(check_first())) has_end;
static_assert(has_end::value, "PartsContainer doesn't provide end() method");
Expand Down
5 changes: 3 additions & 2 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ template <typename OutputIterator> void copy(char ch, OutputIterator out) {
template <typename T> class is_like_std_string {
template <typename U>
static auto check(U* p)
-> decltype(p->find('a'), p->length(), p->data(), int());
-> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
template <typename> static void check(...);

public:
Expand Down Expand Up @@ -106,7 +106,8 @@ template <typename T> class is_tuple_like_ {
template <typename U>
static auto check(U* p)
-> decltype(std::tuple_size<U>::value,
internal::declval<typename std::tuple_element<0, U>::type>(),
(void)internal::declval<
typename std::tuple_element<0, U>::type>(),
int());
template <typename> static void check(...);

Expand Down

0 comments on commit 430e6ac

Please sign in to comment.