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
09fde7f4
Commit
09fde7f4
authored
Jan 22, 2022
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add fmt::underlying for enum classes
parent
0014024a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
1 deletion
+25
-1
doc/api.rst
doc/api.rst
+2
-0
include/fmt/format.h
include/fmt/format.h
+16
-0
test/format-test.cc
test/format-test.cc
+7
-1
No files found.
doc/api.rst
View file @
09fde7f4
...
...
@@ -321,6 +321,8 @@ Utilities
.. doxygenfunction:: fmt::ptr(const std::unique_ptr<T> &p) -> const void*
.. doxygenfunction:: fmt::ptr(const std::shared_ptr<T> &p) -> const void*
.. doxygenfunction:: fmt::underlying(Enum e) -> typename std::underlying_type<Enum>::type
.. doxygenfunction:: fmt::to_string(const T &value) -> std::string
.. doxygenfunction:: fmt::to_string_view(const Char *s) -> basic_string_view<Char>
...
...
include/fmt/format.h
View file @
09fde7f4
...
...
@@ -2675,6 +2675,22 @@ template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
return
p
.
get
();
}
/**
\rst
Converts ``e`` to the underlying type.
**Example**::
enum class color { red, green, blue };
auto s = fmt::format("{}", fmt::underlying(color::red));
\endrst
*/
template
<
typename
Enum
>
constexpr
auto
underlying
(
Enum
e
)
noexcept
->
typename
std
::
underlying_type
<
Enum
>::
type
{
return
static_cast
<
typename
std
::
underlying_type
<
Enum
>::
type
>
(
e
);
}
class
bytes
{
private:
string_view
data_
;
...
...
test/format-test.cc
View file @
09fde7f4
...
...
@@ -1409,8 +1409,14 @@ TEST(format_test, format_pointer) {
EXPECT_EQ
(
"0x0"
,
fmt
::
format
(
"{}"
,
nullptr
));
}
enum
class
color
{
red
,
green
,
blue
};
TEST
(
format_test
,
format_enum_class
)
{
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
fmt
::
underlying
(
color
::
red
)),
"0"
);
}
TEST
(
format_test
,
format_string
)
{
EXPECT_EQ
(
"test"
,
fmt
::
format
(
"{0}"
,
std
::
string
(
"test"
))
);
EXPECT_EQ
(
fmt
::
format
(
"{0}"
,
std
::
string
(
"test"
)),
"test"
);
EXPECT_THROW
((
void
)
fmt
::
format
(
fmt
::
runtime
(
"{:x}"
),
std
::
string
(
"test"
)),
fmt
::
format_error
);
}
...
...
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