将一个枚举数组转换为int的指针

时间:2023-01-19 16:22:13

I'm getting the complaint from complier when trying to convert a array of enum to an pointer of int.

在尝试将枚举数组转换为int指针时,我从complier得到了抱怨。

void format(const int *values);
// convert and call format
format(static_cast<const int*>(EnumArray));

// error from compiler
error: invalid static_cast from type 'const EnumArray[15]' to type 'const int*'

Any way to get around it? Thanks!

有办法解决它吗?谢谢!

2 个解决方案

#1


Seems I can solve it with template. It compiles and runs.

似乎我可以用模板解决它。它编译并运行。

    template<typename T>
    String8  format(const T *values)
    {
          //use values as array of int
          int v = values[i];
    }

    //call it
    format(EnumArray); // no type needed since it can be deduced

#2


If you exactly know what you are doing, you can use reinterpret_cast.

如果您确切知道自己在做什么,可以使用reinterpret_cast。

format(reinterpret_cast<const int*>(EnumArray));

#1


Seems I can solve it with template. It compiles and runs.

似乎我可以用模板解决它。它编译并运行。

    template<typename T>
    String8  format(const T *values)
    {
          //use values as array of int
          int v = values[i];
    }

    //call it
    format(EnumArray); // no type needed since it can be deduced

#2


If you exactly know what you are doing, you can use reinterpret_cast.

如果您确切知道自己在做什么,可以使用reinterpret_cast。

format(reinterpret_cast<const int*>(EnumArray));