Search code examples
c++variable-assignment

Assigning consecutive integers to a list of variables


How to rewrite the following in a more compact form?

constexpr unsigned int dog=1;
constexpr unsigned int cat=2;
constexpr unsigned int elephant=3;
...
constexpr unsigned int zebra=1000;

Ideally, I'd like something like

dog,cat, elephant, ..., zebra = range(1,1000)

Solution

  • Assigning consecutive integers to a list of variables

    They're not really variables if they can't vary.

    You can just replace

    constexpr unsigned int dog=1;
    constexpr unsigned int cat=2;
    constexpr unsigned int elephant=3;
    ...
    constexpr unsigned int zebra=1000;
    

    with

    enum Animal: unsigned {
      dog=1,
      cat,
      elephant,
      ...
      zebra
    };
    

    if that's all you want.