Skip to content

Commit baa7ee3

Browse files
committed
enum_signed
1 parent 6289271 commit baa7ee3

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎cpp/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1. [goto crosses initialization of](goto-cross-initialization.cpp)
1111
1. [const](const.cpp)
1212
1. [enum](enum.cpp)
13+
1. [enum_signed.cpp](enum_signed.cpp)
1314
1. [RTTI](rtti.md)
1415
1. [Value category](value_category.cpp)
1516
1. Compile time magic

‎cpp/enum_signed.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://stackoverflow.com/questions/159034/are-c-enums-signed-or-unsigned/54572423#54572423
2+
3+
#include <cassert>
4+
#include <iostream>
5+
#include <type_traits>
6+
7+
enum Unscoped {};
8+
enum class ScopedDefault {};
9+
enum class ScopedExplicit : long {};
10+
11+
int main() {
12+
std::cout << std::is_signed<std::underlying_type<Unscoped>>() << std::endl;
13+
assert((std::is_same<std::underlying_type<ScopedDefault>::type, int>()));
14+
assert((std::is_same<std::underlying_type<ScopedExplicit>::type, long>()));
15+
}

0 commit comments

Comments
 (0)