0

I have this code which causes a compiler error telling:

Error (active)  E0511   this operation on an enumerated type requires an applicable user-defined operator function
enum e{ val = 201, val2 = 11 };
    for (unsigned bit = 1; e::val; e::val /= e::val2, bit <<= 1) {
        std::cout << bit << std::endl;

why I can't do this operation, enum's type is integer

  • 2
    You are trying to modify a constant value. Does this answer your question? https://stackoverflow.com/questions/10305315/change-enum-values-at-runtime – DNT May 22 '20 at 07:35
  • yes, thank you! how to deal with magic number then? to avoid magic numbers what can I use? –  May 22 '20 at 07:40
  • you give them a name. Your enum is as magic as a bare `201` btw. Try `const int some_meaningful_name = 201;` – 463035818_is_not_an_ai May 22 '20 at 07:45
  • @idclev463035818, if I use const I can't change their value at runtime –  May 22 '20 at 07:46
  • @Ope I believe there are several suggestions in the link I posted. One is to use a `struct` instead of `enum` – DNT May 22 '20 at 07:48
  • 1
    you are mixing up things. If `201` is merely the initializer for a variable called `val` then there is nothing magic and also you don't need an enum. Maybe it would be good if you added a line or two about what you actually want to achieve. – 463035818_is_not_an_ai May 22 '20 at 07:50
  • @idclev463035818, I'm mixing nothing, you said use const int, I told you const value cannot change in runtime? where's the error in what I said? –  May 22 '20 at 07:56
  • no reason to feel offended. Mixing up: You are using an enum but you want to modify one of the enums value. That doesnt work. More mixing up: You say you want to avoid a magic number, but the initial value for a variable is not commonly considered as "magic", because if you do you end up writing non-sense code like `const int val1_init = 201; int val1 = val1_init;`. There is no magic in `int val1 = 201;`. If however the `201` is not only the initializer for `val1`, but you use it elsewhere, then you should clarify your question imho – 463035818_is_not_an_ai May 22 '20 at 08:00
  • @idclev463035818, I know that 201 isn't a magic number but clang consider it as a magic number ```Clang: 201 is a magic number; consider replacing it with a named constant``` –  May 22 '20 at 08:03
  • no way to tell if the warning is justified without seeing the code that triggered it. Acutally that would be a different question. – 463035818_is_not_an_ai May 22 '20 at 08:09
  • I guess what @idclev463035818 refers to as magic number is the following: "A magic number is a numeric literal (for example, 8080, 2048) that is used in the middle of a block of code without explanation." ([Link](https://help.semmle.com/wiki/display/CCPPOBJ/Magic+numbers)). – StefanKssmr May 22 '20 at 08:09
  • @idclev463035818, here's the code ```int m_n = 201, m_d = 21; for (unsigned bit = 1;m_n;m_n /= m_d, bit <<= 1) std::cout << bit << std::endl;``` –  May 22 '20 at 08:12
  • ok, I considered the warning and concluded that `201` is not magic here. As I wrote above, sometimes an initializer is just an initializer. `const int m_n_initial = 201; int m_n = m_n_initial;` would be a tiny bit less magic, but non-sense (unless you use `m_n_initial` elsewhere) – 463035818_is_not_an_ai May 22 '20 at 08:14
  • @idclev463035818, be straightforward, why talking about other things, Clang is incorrect for you? –  May 22 '20 at 08:16
  • 1
    hum? A warning is just a warning. If this is an error you should reconfigure clang, because it shouldn't be an error. I am not saying that Clang is incorrect, not at all. I appreciate its warning, consider it, and decide to leave the code as is. (I would put the definitions of `m_n` and `m_d` on separate lines, but thats a different story) – 463035818_is_not_an_ai May 22 '20 at 08:18
  • fwiw, if you had included the motivation for writing the code in your question into the question we didn't need the discussion in comments. It would have made the question easier to answer. Don't get me wrong, not critizising, just a tip – 463035818_is_not_an_ai May 22 '20 at 08:23
  • @idclev463035818, okay clang :D –  May 22 '20 at 08:33
  • _"enum's type is integer"_ No, its type is `enum e`. – Asteroids With Wings May 22 '20 at 10:23

1 Answers1

0

As DNT already pointed out in the comments, one problem is, you try to modify a constant value.

Another problem is that the type of the elements in your enumartion is e not int. See c++ reference. For the type e, you have to overload operator/= . If you want to use int's operator you can do the following:

#include <iostream>

enum e{ val = 201, val2 = 11 };

int main()
{
    int eval = e::val;
    for (unsigned bit = 1; eval; eval /= e::val2, bit <<= 1) {
        std::cout << bit << std::endl;
    }
}
StefanKssmr
  • 1,196
  • 9
  • 16