指向 VS2015 Update 2 中非静态数据成员的指针不正确

Pointer to a non-static data members incorrect in VS2015 Update 2

本文关键字:数据成员 指针 不正确 静态 VS2015 Update 指向      更新时间:2023-10-16

VS2015 Update 2 中的编译器似乎C++回归 - 当对具有虚拟析构函数的类之一使用多重继承时,计算基类数据成员的不正确添加。代码示例:

// Example program
#include <iostream>
#include <limits>
struct V
{
    virtual ~V() {};
};
struct B
{
    int i;
};
struct A : public B, public V
{
};
int main()
{
    int A::* a1 = &A::i;
    A a;
    a.i = std::numeric_limits<int>::max();
    if (a.*a1 == std::numeric_limits<int>::max())
        std::cout << "okn";
    else
        std::cout << "nokn";
}

看起来 a1 得到了对向表的偏移量而不是 i

此代码用于在VS2015中打印正常,直到更新1和更新2中的nok。我在GCC和Clang打印的支票也可以。

使用指向成员的指针时,多重继承是否有任何限制?

回答我自己的问题,以防有人遇到同样的问题:更新 2 中确实存在回归,正如 MSDN 论坛上所讨论的那样。基本上你有两个选择:

  1. 使用/vmg编译器开关(这将增加二进制文件的大小)。
  2. 通过基类&B::i获取正确的偏移量,并将其分配给派生类A