Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
56823388
Unverified
Commit
56823388
authored
Jun 19, 2022
by
jehelset
Committed by
GitHub
Jun 19, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix is_formattable for tuple-like types. (#2940)
parent
f0de1284
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
2 deletions
+44
-2
include/fmt/ranges.h
include/fmt/ranges.h
+33
-1
test/ranges-test.cc
test/ranges-test.cc
+11
-1
No files found.
include/fmt/ranges.h
View file @
56823388
...
...
@@ -202,6 +202,31 @@ template <size_t N>
using
make_index_sequence
=
make_integer_sequence
<
size_t
,
N
>
;
#endif
template
<
typename
T
>
using
tuple_index_sequence
=
make_index_sequence
<
std
::
tuple_size
<
T
>::
value
>
;
template
<
typename
T
,
typename
C
,
bool
=
is_tuple_like_
<
T
>
::
value
>
class
is_tuple_formattable_
{
public:
static
constexpr
const
bool
value
=
false
;
};
template
<
typename
T
,
typename
C
>
class
is_tuple_formattable_
<
T
,
C
,
true
>
{
template
<
std
::
size_t
...
I
>
static
std
::
true_type
check2
(
index_sequence
<
I
...
>
,
integer_sequence
<
bool
,
(
I
==
I
)...
>
);
static
std
::
false_type
check2
(...);
template
<
std
::
size_t
...
I
>
static
decltype
(
check2
(
index_sequence
<
I
...
>
{},
integer_sequence
<
bool
,
(
is_formattable
<
typename
std
::
tuple_element
<
I
,
T
>::
type
,
C
>::
value
)...
>
{}))
check
(
index_sequence
<
I
...
>
);
public:
static
constexpr
const
bool
value
=
decltype
(
check
(
tuple_index_sequence
<
T
>
{}))
::
value
;
};
template
<
class
Tuple
,
class
F
,
size_t
...
Is
>
void
for_each
(
index_sequence
<
Is
...
>
,
Tuple
&&
tup
,
F
&&
f
)
noexcept
{
using
std
::
get
;
...
...
@@ -283,8 +308,15 @@ template <typename T> struct is_tuple_like {
detail
::
is_tuple_like_
<
T
>::
value
&&
!
detail
::
is_range_
<
T
>::
value
;
};
template
<
typename
T
,
typename
C
>
struct
is_tuple_formattable
{
static
constexpr
const
bool
value
=
detail
::
is_tuple_formattable_
<
T
,
C
>::
value
;
};
template
<
typename
TupleT
,
typename
Char
>
struct
formatter
<
TupleT
,
Char
,
enable_if_t
<
fmt
::
is_tuple_like
<
TupleT
>::
value
>>
{
struct
formatter
<
TupleT
,
Char
,
enable_if_t
<
fmt
::
is_tuple_like
<
TupleT
>::
value
&&
fmt
::
is_tuple_formattable
<
TupleT
,
Char
>::
value
>>
{
private:
// C++11 generic lambda for format().
template
<
typename
FormatContext
>
struct
format_each
{
...
...
test/ranges-test.cc
View file @
56823388
...
...
@@ -85,11 +85,22 @@ TEST(ranges_test, format_pair) {
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
p
),
"(42, 1.5)"
);
}
struct
unformattable
{};
TEST
(
ranges_test
,
format_tuple
)
{
auto
t
=
std
::
tuple
<
int
,
float
,
std
::
string
,
char
>
(
42
,
1.5
f
,
"this is tuple"
,
'i'
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
t
),
"(42, 1.5,
\"
this is tuple
\"
, 'i')"
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
std
::
tuple
<>
()),
"()"
);
EXPECT_TRUE
((
fmt
::
is_formattable
<
std
::
tuple
<>>::
value
));
EXPECT_FALSE
((
fmt
::
is_formattable
<
unformattable
>::
value
));
EXPECT_FALSE
((
fmt
::
is_formattable
<
std
::
tuple
<
unformattable
>>::
value
));
EXPECT_FALSE
((
fmt
::
is_formattable
<
std
::
tuple
<
unformattable
,
int
>>::
value
));
EXPECT_FALSE
((
fmt
::
is_formattable
<
std
::
tuple
<
int
,
unformattable
>>::
value
));
EXPECT_FALSE
(
(
fmt
::
is_formattable
<
std
::
tuple
<
unformattable
,
unformattable
>>::
value
));
EXPECT_TRUE
((
fmt
::
is_formattable
<
std
::
tuple
<
int
,
float
>>::
value
));
}
#ifdef FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
...
...
@@ -220,7 +231,6 @@ TEST(ranges_test, enum_range) {
}
#if !FMT_MSC_VERSION
struct
unformattable
{};
TEST
(
ranges_test
,
unformattable_range
)
{
EXPECT_FALSE
((
fmt
::
has_formatter
<
std
::
vector
<
unformattable
>
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment