Skip to content

Commit fbfaf84

Browse files
committed
improve enum stuff a bit
1 parent bf6e519 commit fbfaf84

3 files changed

Lines changed: 19 additions & 26 deletions

File tree

cpp/enum.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/*
2-
Simple rule: always use C++11 enum class, never the old C-style enums.
3-
4-
- http://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum
5-
- http://stackoverflow.com/questions/4506750/how-do-i-use-the-enum-value-from-a-class-in-another-part-of-code
6-
- http://stackoverflow.com/questions/10869790/best-practices-for-enum-in-c
7-
*/
1+
// Simple rule: always use C++11 enum class, never the old C-style enums.
2+
//
3+
// - http://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum
4+
// - http://stackoverflow.com/questions/4506750/how-do-i-use-the-enum-value-from-a-class-in-another-part-of-code
5+
// - http://stackoverflow.com/questions/10869790/best-practices-for-enum-in-c
86

97
#include "common.hpp"
108

@@ -22,11 +20,11 @@ int main() {
2220
assert(sizeof(E) == sizeof(char));
2321
}
2422

25-
// # enum class
23+
// # scoped enumeration
2624
//
27-
// Much better than the older enum, just always use it.
25+
// Much better than the older raw enum, just always use it.
2826
{
29-
enum class ClassEnum {
27+
enum class ClassEnum {
3028
A,
3129
B
3230
};
@@ -40,13 +38,11 @@ int main() {
4038
//int i = ClassEnum::A;
4139
}
4240

43-
/*
44-
# enum to string
45-
46-
C++14 nope:
47-
48-
- http://stackoverflow.com/questions/201593/is-there-a-simple-way-to-convert-c-enum-to-string
49-
- http://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c-and-future-c17-c20
50-
*/
41+
// # enum to string
42+
//
43+
// C++14 nope:
44+
//
45+
// - http://stackoverflow.com/questions/201593/is-there-a-simple-way-to-convert-c-enum-to-string
46+
// - http://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c-and-future-c17-c20
5147
#endif
5248
}

cpp/enum_signed.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ enum class ScopedDefault {};
99
enum class ScopedExplicit : long {};
1010

1111
int main() {
12+
// Implementation defined, let's find out.
1213
std::cout << std::is_signed<std::underlying_type<Unscoped>>() << std::endl;
14+
15+
// Guaranteed. Scoped defaults to int.
1316
assert((std::is_same<std::underlying_type<ScopedDefault>::type, int>()));
17+
18+
// Guaranteed. We set it ourselves.
1419
assert((std::is_same<std::underlying_type<ScopedExplicit>::type, long>()));
1520
}

cpp/template.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,6 @@ int main() {
722722
#endif
723723
}
724724

725-
/*
726-
> inside template
727-
728-
typename std::enable_if<sizeof(T) > 1, void>::type
729-
730-
http://stackoverflow.com/questions/33423702/how-to-use-greater-than-inside-a-template-parameter-and-not-get-a-parsing-er
731-
*/
732-
733725
/*
734726
>=
735727

0 commit comments

Comments
 (0)