example_containers_bitset.cpp
Go to the documentation of this file.
1 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
2 // SPDX-License-Identifier: MIT
3 // Copyright (c) 2019 - 2024 Daniil Goncharov <neargye@gmail.com>.
4 // Copyright (c) 2022 - 2023 Bela Schaum <schaumb@gmail.com>.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 
24 #if defined(_MSC_VER)
25 # pragma warning(push)
26 # pragma warning(disable : 4244) // warning C4244: 'argument': conversion from 'const T' to 'unsigned int', possible loss of data.
27 #endif
28 
29 #include <iostream>
30 
32 
33 enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
34 template <>
36  static constexpr bool is_flags = true;
37 };
38 
39 int main() {
40 
41  auto color_bitset = magic_enum::containers::bitset<Color>();
42  color_bitset.set(Color::GREEN);
43  color_bitset.set(Color::BLUE);
44 
45  std::cout << std::boolalpha;
46  std::cout << color_bitset.size() << std::endl; // 3 == magic_enum::enum_count<Color>()
47  std::cout << color_bitset.all() << std::endl; // false
48  std::cout << color_bitset.any() << std::endl; // true
49  std::cout << color_bitset.none() << std::endl; // false
50  std::cout << color_bitset.count() << std::endl; // 2
51  std::cout << color_bitset.test(Color::RED) << std::endl; // false
52  std::cout << color_bitset.test(Color::GREEN) << std::endl; // true
53  std::cout << color_bitset.test(Color::BLUE) << std::endl; // true
54 
55  return 0;
56 }
Color::GREEN
@ GREEN
magic_enum::containers::bitset
Definition: magic_enum_containers.hpp:485
main
int main()
Definition: example_containers_bitset.cpp:39
Catch::cout
std::ostream & cout()
magic_enum_containers.hpp
Color::RED
@ RED
magic_enum::customize::enum_range
Definition: magic_enum.hpp:172
Color
Color
Definition: example.cpp:28
Color::BLUE
@ BLUE


magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:18