BWE_CO-OP
BWE_CO-OP

Reputation: 1

Function that returns a cast enumerated value C++

I'm trying to make a class using enumerated values for a position of a sensor and I am using vectors with a type int as the input for this function, and I want an enumerated value out. I'm not sure if this code will work or not. I'm not quite sure how to test it.

#include <vector>

place getPos(vector<int>& pin)
    {
        int i;
    for(i = 0; i <= sizeof(pin); i++)
    {
    if (pin[i])
        break;
    }

    place castEnum = (place)i;
    return castEnum;

    }

So this is the update as far as I can gather:

#include <vector>

place getPos(vector<int>& pin)
{
    int i;
    for(i = 0; i <= pin.size(); i++)
    {
        if (pin[i])
            break;
    }
    return static_cast <place> (i);

}

Upvotes: 0

Views: 100

Answers (3)

user2249683
user2249683

Reputation:

Just fixing the obvious:

#include <vector>

place getPos(vector<int>& pin)
{
    int i;
    // sizeof(pin) is a constant representing the size of a vector type
    // Edit: missed that for(i = 0; i <= pin.size(); i++)
    for(i = 0; i < pin.size(); ++i)
    {
        if (pin[i])
            break;
    }
    return place(i);
}

Not so obvious:

You may replace your vector with a std::bitset (or an unsigned integer)
representing the flags in your enum.

Upvotes: 1

Zac Howland
Zac Howland

Reputation: 15872

To address the errors and logical issues:

#include <vector>

place getPos(vector<int>& pin)
{
    int i;
    for(i = 0; i < pin.size(); i++)
   //            ^ Needs to be <, not <=
   //              ^^^^^^^^^^ Needs to be pins.size(), not sizeof(pins) 
    {
        if (pin[i])
            break;
    }

    // alternatively,
    //int i = std::find_if(pins.begin(), pins.end(), [](int a)
    //{
    //    return a != 0;
    //});

    place castEnum = DEFAULT_ENUM_VALUE; // whatever you want for a default enum value
    switch (i)
    {
    case ENUM_VALUE_1:
        castEnum = ENUM_VALUE_1;
        break;
    case ENUM_VALUE_2:
        castEnum = ENUM_VALUE_2;
        break;
    // etc.
    }
    return castEnum;
}

Using the switch statement avoids issues with trying to cast values that are not valid for your enumeration. You will need to define an enumeration value (e.g. INVALID_ENUM = -1) as a default value.

Upvotes: 0

John Dibling
John Dibling

Reputation: 101456

An integral value may be cast to an enum by using static_cast:

enum FooType
{
  ftOne = 1,
  ftTwo
};

int main()
{
  const int n = 1;
  FooType ft = static_cast <FooType> (n);
}

Since static_cast is a compile-time operation and n is known only at run-time, if n does not match one of the enumerated values then the resulting value is unspecified. This can be a serious problem.

You will need to determine beforehand that the value you're about to cast is a legitimate value for that enum. When you think about this, usually this defeats the purpose of doing the cast in the first place.

This is often an indication that you're doing something wrong. Why would you need to cast an integral value to an enum? One possible reason is because you pulled that value off of a socket or got it from some other means of interprocess communication. Aside from that no other valid use cases come immediately to mind. This is a classic indication of an XY Problem.

Upvotes: 3

Related Questions