假设我有一个包含两个数组成员的类,元素类型相同但大小不同:

struct X
{
  string a[2];
  string b[3];
};

constexpr auto array_members = std::array{ &X::a, &X::b };

这不会编译,因为两个数组(具有不同长度)具有不兼容的类型。

是否存在可以将两个成员指针分配给的公共(public)类型?

我也尝试过 static_cast<string (X::*)[]>(&X::a) 但由于数组类型不完整,这也无法编译。

我不能使用 offsetof 因为它需要使用非标准布局类。

这可能是一种解决方法:
using array_type = decltype(X::b);
auto const array_members = std::array{
  reinterpret_cast<array_type X::*>(&X::a), // cannot be constexpr
  &X::b
};

但我担心调用未定义的行为。尽管我确信在运行时没有越界元素引用,但我们确实创建了指向 a 的最后一个指针,鉴于 b 的类型似乎是在界内。我不确定这是否违反规范。如果我可以使用与 constexpr 不兼容的 reinterpret_cast 也会很方便。

最佳答案

您不能拥有指向不同类型成员的成员指针数组。

除了成员指针,您还可以使用某种函数将 span 返回给成员:

template<auto m>
constexpr auto getter = [](X& x) noexcept
                        // X could be deduced from m for extra genericity
                        // https://stackoverflow.com/questions/25228958
{

    return span{x.*m, std::size(x.*m)};
};

constexpr auto array_members = std::array{ +getter<&X::a>, +getter<&X::b> };

No assembly generated :) 在零优化级别(当然,直到您实际调用函数)。 array_members 是一个函数指针数组。用法示例:
X x;
span a = array_members[0](x);
a[0] = "test";

这使用了 span ,它不在 C++17 标准库中,因此您需要使用它的另一个实现。

关于c++ - 指向不同大小成员数组的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58239838/

10-11 22:02
查看更多