Tags down


How can I input a state to a variable of enum type in C++?

By : a.b.r
Date : July 30 2020, 05:00 PM
this one helps. You can't use cin direct to enum type, but you can do it to an int and static cast it to your enum type like shown below:
code :
#include <iostream>

enum Day : uint16_t { Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun };

void printa(Day day) {
     if (day > Fri)
        std::cout << "weekend" << std::endl;
        std::cout << "weekdays" << std::endl;

int main() {
    uint16_t day = Sun;

    std::cin >> day;


    return 0;

Share : facebook icon twitter icon

Can the methods in an Enum type change the state of the instances of an Enum?

By : David Hernandez
Date : March 29 2020, 07:55 AM
Hope this helps Java enums are really just classes with some special treatment by the compiler and runtime. So yes, method calls on an enum instance can certainly change its state. I'm not sure what you mean with "then the invariant of an Enum type ie some type that exports a few constants will break".
Who says enums can only be a bunch of constants? The essence of an enum type is that you have a predefined fixed number of instances which you can refer to by name - and Java enums implement that. But why would a language not be allowed to have enums that are more powerful than that?

Creating Enum variable from a dynamic type that I know is an Enum?

By : imcymon
Date : March 29 2020, 07:55 AM
may help you . Enum.ToObject(Type,object) solved this.
For example after I confirmed that type is a System.Enum then I can do (System.Enum)Enum.ToObject(type,0).

Can I use the pattern matching operator ~= to match an enum value to an enum type with an associated variable?

By : user3643340
Date : March 29 2020, 07:55 AM
will help you From the documentation for Swift 1.2 "Enumeration case patterns appear only in switch statement case labels". So yes, you need to define your ~= operator (as from the answer pointed in the comments).
In case you just need isA and isB you can implement them using switch combined with _. In your case ~= couldn't work out of the box anyway, since you use an Any associated type, which is not Equatable (i.e.: how can I say if two .B(any) are equal since I cannot say if two any are equal?)
code :
enum MyEnum {
    case A, B(object: String)

let myEnumA = MyEnum.A
let myEnumB = MyEnum.B(object: "Foo")

func ~=(lhs: MyEnum, rhs: MyEnum) -> Bool {
    switch (lhs, rhs) {
    case (.A, .A):
        return true
    case let (.B(left), .B(right)):
        return left == right
        return false

myEnumA ~= .A   // true
myEnumB ~= .B(object: "Foo") // true
myEnumB ~= .B(object: "Bar") // false

func isA(value: MyEnum) -> Bool {
    switch value {
    case .A:
        return true
        return false

func isB(value: MyEnum) -> Bool {
    switch value {
    case .B(_):     // Ignore the associated value
        return true
        return false

Why does 'typeof enum constant' generate a warning when compared to a variable of enum type?

By : user3886605
Date : March 29 2020, 07:55 AM
I wish this help you Quoting directly from C11, chapter §, Enumeration specifiers,

Why do I have to cast an enum element when assigning it to a same enum variable type in C?

By : Kavi Solitarius Puer
Date : March 29 2020, 07:55 AM
Any of those help (Hi, this is a new account so I cannot use the comments section yet to ask for further clarification, so, my answer may be broader than needed)
Based on the text of the warning message I assume you are talking about MISRA-C:2012 (the latest standard) which is a great improvement over the prior ones in that much more effort in stating the rationale along with many more compliant and non-compliant examples have been added. This being Rule 10.3, the rationale is: since C permits assignments between different arithmetic types to be performed automatically, the use of these implicit conversions can lead to unintended results, with the potential for loss of value, sign or precision.
code :
enum enuma { A1, A2, A3   } ena;
ena  = A1;
Related Posts Related Posts :
  • Sending argument to an .exe via SHELLEXECUTEINFO
  • Why I can't use i/10 in FOR LOOP, C++?
  • How to use PERF_SAMPLE_READ with mmap
  • Is there any way to iterate through a struct?
  • My character counting code is miscounting characters. Why?
  • Difference between std::resize(n) and std::shrink_to_fit in C++?
  • Wildcard for C++ concepts saying "accepting anything for this template argument"
  • Which of the objects of the class A would be created first in this code snippet?
  • Floating point math accuracy, c++ vs fortran
  • C++ initialize objects with packed array of integers
  • Picking a constructor to call
  • using function call in constructor gives different result c++
  • Using strcpy to copy elements of an char array to another array
  • Templated Constructor Specialization
  • Struggling to convert an integer into a linked list. Not sure what I'm doing wrong here
  • C++: Is it better to store a console output in a macro?
  • C++ - Exception in Constructor
  • AVX: matrix dot vector, but ignore diagonal
  • Pointer To Pointer passed by reference
  • Why is this operator< overload function invisible to STL algorithms?
  • What is the correct algorithm to perform double-float division?
  • In the case of using a std::unique_ptr to automatically deallocate memory upon exiting a scoped block, why not just use
  • C++: Iterating over a string vector and using the index for putenv
  • OpenCL Matrix Multiplication Using std::vector
  • template template parameter deduction: three different compilers three different behaviors
  • C++ pull a card from deck
  • File reading with and without new line
  • store strings in stable memory in c++
  • Why is static_cast used in QT's official document
  • iterator .end() from std::list returns "0xcdcdcdcdcdcdcdcd" but .begin() as expected
  • Is std::sqrt the same as sqrt in C++
  • Iterate through std::initializer_list
  • Codeblocks c++ code doesn't run in VS 19 (vector subscript out of range)
  • Why erase on std::vector promote iterator
  • how to destroy an object in C++
  • How to create Visual Studio 2015 solution from CMake
  • Using concepts for checking if a type T has a field F
  • constructor giving error: no matching function for call to
  • search top n% of a vector member C++
  • Is this reinterpret_cast problematic in principle, and/or in practice?
  • What is the following error doing? I do not see any error with the bounds
  • Two index's of an array have same memory adress
  • how do i avoid integer division in c++?
  • Setting value to enum
  • Properties of a pointer to a zero length array
  • Why the output is different how ever the formula is the same?
  • Restarting from the beginning using do while loops
  • Error: invalid initialization of reference of type
  • how to avoid to use memcpy to create a string
  • Copying the vector elements into std::array
  • Is there any class pointers in Perl in order to access member variables and functions?
  • Why the static variable is not set to 1000
  • Why can't a destructor have reference qualifiers?
  • How to print binary tree in horizontal way?
  • Comparison behavior of ' <' operator
  • Constructors in C++ Language
  • Why displayed value of b , c and y is different from my trace?
  • Undefined reference to `fftw_init_threads' while installing pHash library
  • Reading remapped hdd sectors
  • why the result is 1?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org