Skip to content

Commit 4bace71

Browse files
committed
clarify overloading int with Style
1 parent 367285c commit 4bace71

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,15 @@ we will define static variables for each enum entry.
375375
* Inspired by the [memory_order change in the standard](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0439r0.html)
376376

377377
* If the enum was used as bit flags, define bitwise operators as well.
378+
And if there were methods that recieved the unscoped enum as `int`,
379+
overload them to receive the scoped enum now (since the bitwise operators return the scoped enum,
380+
if they were to return an `int`, users will not be able to chain more than 2 of them: e.g.
381+
`Print(STYLE_BOLD | STYLE_ITALLIC | STYLE_STRIKE_THROUGH) // operator|(Style, int) is not overloaded`).
378382

379383
```cpp
380384
// Add `friend` if the enum lies inside a `struct`
381385
[friend] inline Style operator|(Style lhs, Style rhs) {
382-
return Style{static_cast<int>(lhs) | static_cast<int>(rhs)};
386+
return static_cast<Style>(static_cast<int>(lhs) | static_cast<int>(rhs));
383387
}
384388
```
385389

some_unstable_lib/include/ChangeToEnumClass.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#pragma once
2+
#include <string> // string_view
3+
#include <type_traits> // underlying_type_t
24

35
struct Text {
46
#ifndef BC_API_CHANGED
@@ -24,7 +26,13 @@ struct Text {
2426
// If the enum was used as bit flags, define bitwise operators as well.
2527
friend inline Style operator|(Style lhs, Style rhs) {
2628
using ut = std::underlying_type_t<Style>;
27-
return Style{static_cast<ut>(lhs) | static_cast<ut>(rhs)};
29+
return static_cast<Style>(static_cast<ut>(lhs) | static_cast<ut>(rhs));
2830
}
2931
#endif
3032
};
33+
34+
// If you have methods that received Style as int, overload them to receive Style
35+
void Print(std::string_view /*text*/, int /*style*/) {}
36+
#ifdef BC_API_CHANGED
37+
void Print(std::string_view /*text*/, Text::Style /*style*/) {}
38+
#endif

tests/ChangeToEnumClassTest.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
#include <string>
21
#include "ChangeToEnumClass.hpp"
32
#include "gtest/gtest.h"
43

5-
void Print(std::string_view /*text*/, Text::Style /*style*/) {}
6-
74
TEST(ChangeToEnumClass, Basic) {
8-
Print("test", {});
5+
Print("test", 0);
96
Print("test", Text::STYLE_BOLD);
107
Print("test", Text::STYLE_BOLD | Text::Style::STYLE_ITALLIC);
118
Print("test", Text::Style::STYLE_BOLD | Text::STYLE_ITALLIC);
12-
Print("test", Text::Style::STYLE_BOLD | Text::Style::STYLE_ITALLIC);
9+
Print("test", Text::STYLE_BOLD | Text::STYLE_ITALLIC | Text::STYLE_STRIKE_THROUGH);
10+
Print("test",
11+
Text::Style::STYLE_BOLD | Text::Style::STYLE_ITALLIC | Text::Style::STYLE_STRIKE_THROUGH);
1312
}

0 commit comments

Comments
 (0)