在return语句中将指针强制转换为固定大小的数组

时间:2021-02-21 18:41:27

The simplest way to ask this question is with some code:

提出这个问题的最简单方法是使用一些代码:

struct Point
{
    int x;
    int y;
    int z;

    int* as_pointer() { return &x; }        // works
    int (&as_array_ref())[3] { return &x; } // does not work   
};

as_pointer compiles, as_array_ref does not. A cast seems to be in order but I can't figure out the appropriate syntax. Any ideas?

as_pointer编译,as_array_ref没有。演员似乎是有序的,但我无法弄清楚合适的语法。有任何想法吗?

3 个解决方案

#1


7  

I find that array types are easier to deal with with a typedef:

我发现使用typedef更容易处理数组类型:

typedef int ints[3];

Then your as_array_ref must be written so that &as_array_ref() == &x.

然后必须编写as_array_ref,以便&as_array_ref()==&x。

The following syntaxes are possible:

可以使用以下语法:

  1. plain C-style cast from int* to ints*:

    从int *到ints *的纯C样式转换:

    ints& as_array_ref() { return *( (ints*)(&x) ); }

    ints&as_array_ref(){return *((ints *)(&x)); }

  2. C++ style reinterpret_cast (suggested by @Mike Seymour - see also his answer). It is often considered a better practice in C++:

    C ++风格reinterpret_cast(由@Mike Seymour建议 - 另见他的回答)。它通常被认为是C ++中的一种更好的做法:

    ints& as_array_ref() { return *reinterpret_cast<ints*>(&x); }

    ints&as_array_ref(){return * reinterpret_cast (&x); }

  3. Cast from int& to ints& which is slightly shorter but (for me) less intuitive:

    从int转到int并且稍微缩短但是(对我来说)不那么直观:

    ints& as_array_ref() { return reinterpret_cast<ints&>(x); }

    ints&as_array_ref(){return reinterpret_cast (x); } &>

#2


4  

The cast you need to reinterpret a reference to a variable as a reference to an array is:

需要重新解释对变量的引用作为对数组的引用的转换是:

reinterpret_cast<int(&)[3]>(x);

Be aware that using this gives undefined behaviour; it will probably work on any sensible implementation, but there's no guarantee that there won't be padding between the members of the class, while arrays are not padded.

请注意,使用此方法会产生未定义的行为;它可能适用于任何合理的实现,但不能保证类的成员之间不会有填充,而数组没有填充。

#3


2  

I think that what you're trying to do would be easier (and clearer/cleaner) with an union.

我认为你想要做的事情会更容易(更清晰/更清洁)。

#1


7  

I find that array types are easier to deal with with a typedef:

我发现使用typedef更容易处理数组类型:

typedef int ints[3];

Then your as_array_ref must be written so that &as_array_ref() == &x.

然后必须编写as_array_ref,以便&as_array_ref()==&x。

The following syntaxes are possible:

可以使用以下语法:

  1. plain C-style cast from int* to ints*:

    从int *到ints *的纯C样式转换:

    ints& as_array_ref() { return *( (ints*)(&x) ); }

    ints&as_array_ref(){return *((ints *)(&x)); }

  2. C++ style reinterpret_cast (suggested by @Mike Seymour - see also his answer). It is often considered a better practice in C++:

    C ++风格reinterpret_cast(由@Mike Seymour建议 - 另见他的回答)。它通常被认为是C ++中的一种更好的做法:

    ints& as_array_ref() { return *reinterpret_cast<ints*>(&x); }

    ints&as_array_ref(){return * reinterpret_cast (&x); }

  3. Cast from int& to ints& which is slightly shorter but (for me) less intuitive:

    从int转到int并且稍微缩短但是(对我来说)不那么直观:

    ints& as_array_ref() { return reinterpret_cast<ints&>(x); }

    ints&as_array_ref(){return reinterpret_cast (x); } &>

#2


4  

The cast you need to reinterpret a reference to a variable as a reference to an array is:

需要重新解释对变量的引用作为对数组的引用的转换是:

reinterpret_cast<int(&)[3]>(x);

Be aware that using this gives undefined behaviour; it will probably work on any sensible implementation, but there's no guarantee that there won't be padding between the members of the class, while arrays are not padded.

请注意,使用此方法会产生未定义的行为;它可能适用于任何合理的实现,但不能保证类的成员之间不会有填充,而数组没有填充。

#3


2  

I think that what you're trying to do would be easier (and clearer/cleaner) with an union.

我认为你想要做的事情会更容易(更清晰/更清洁)。