在受保护的匿名联合的匿名结构中定义的成员仍然公开可见
member defined in anonymous struct in protected anonymous union is still publicly visible
我已经编译并运行了c++代码,尽管它不应该。
以下代码段不应编译:
template<typename T, size_t SIZE>
struct Vector {
Vector(std::initializer_list<T> data) {
std::copy(data.begin(), data.end(), this->data);
}
Vector(T(&data)[SIZE]) {
std::copy(data, data + SIZE, this->data);
}
protected:
#pragma pack(push, 1) //stores the alignment of aggregate types and sets it to 1 byte
union {
struct {
T x, y, z, w;
};
T data[SIZE];
};
#pragma pack(pop) //restore old data alignment
};
template<typename T>
struct Vector2 : public Vector<T, 2> {
using Vector<T, 2>::Vector<T, 2>;
Vector2(T x = 0, T y = 0) :
Vector({ x, y }){}
Vector2(const Vector& vec) :
Vector(vec) {}
using Vector::x;
using Vector::y;
};
int main() {
double floats[2]{ 2, 3 };
Vector2<double> v{ floats };
Vector<double, 2> c{ 5., 6. };
std::cout << "v.x = " << v.x;
//Is oke, v.x is visible here because of the public using statement
std::cout << " c.x = " << c.x << "n";
//Is not oke, c is not a Vector2<double>. It is a Vector<double, 2> so its member x is protected and thus not visible from here.
}
输出:v.x = 2 c.x = 5
因此,该程序不仅成功地编译和链接,而且还运行和打印合理的数据。我尝试将c
的类型更改为Vector<double, 3>
,但这并没有改变任何内容。此外,成员z
和w
也是可见的,就像x
和y
一样。但是,data
不可见(例如,std::cout << c.data[0];
不会按预期编译)。
Intellisense在这种情况下比编译器更聪明,因为它成功地检测到错误并对此进行投诉
我正在使用Visual studio 2013。
PS:
附带问题:我在同一个代码片段中发现了编译器的另一个怪癖。如果我更改以下行:
using Vector<T, 2>::Vector<T, 2>;
至:
using Vector<T, 2>::Vector;
我得到这个编译器错误:error C2886: 'Vector<T,0x02>' : symbol cannot be used in a member using-declaration
如果我将其更改为:
using Vector::Vector;
编译器与:fatal error C1001: An internal error has occurred in the compiler. see reference to class template instantiation 'Vector2<T>' being compiled
一起崩溃。
这(例如,它崩溃的事实)可能只是编译器中的一个错误,但如果有人知道的话,我仍然想知道为什么这行的两种替代形式都没有编译。
我不得不做一些更改,才能让代码在clang上编译。visual c++似乎非常宽容,允许使用非法(或者应该说是非标准)语法。
以下是更正后的程序:
#include <iostream>
#include <algorithm>
template<typename T, size_t SIZE>
struct Vector {
Vector(std::initializer_list<T> data) {
std::copy(data.begin(), data.end(), this->data);
}
Vector(T(&data)[SIZE]) {
std::copy(data, data + SIZE, this->data);
}
protected:
#pragma pack(push, 1) //stores the alignment of aggregate types and sets it to 1 byte
union {
struct {
T x, y, z, w;
};
T data[SIZE];
};
#pragma pack(pop) //restore old data alignment
};
template<typename T>
struct Vector2 : public Vector<T, 2> {
using Vector<T, 2>::Vector;
Vector2(T x = 0, T y = 0) :
Vector<T, 2>({ x, y }){}
Vector2(const Vector2& vec) :
Vector<T, 2>(vec) {}
using Vector<T, 2>::x;
using Vector<T, 2>::y;
};
int main() {
double floats[2]{ 2, 3 };
Vector2<double> v{ floats };
Vector<double, 2> c{ 5., 6. };
std::cout << "v.x = " << v.x;
//Is oke, v.x is visible here because of the public using statement
std::cout << " c.x = " << c.x << "n";
//Is not oke, c is not a Vector2<double>. It is a Vector<double, 2> so its member x is protected and thus not visible from here.
}
以下是修改后的(预期)错误:
./vec.cpp:66:33: error: 'x' is a protected member of 'Vector<double, 2>'
std::cout << " c.x = " << c.x << "n";
^
./vec.cpp:37:15: note: declared protected here
T x, y, z, w;
^
1 error generated.
- C++:无法访问声明的受保护成员
- 继承和友元函数,从基类访问受保护的成员
- 为什么派生类的好友不能使用受保护的成员?
- 相同的层次结构,访问基类的受保护成员时的行为不同
- 从模板化父类中的派生内部类访问受保护的成员变量
- 在使用受保护和继承时无法访问在类中声明的私有成员
- 同一模板类但模板类型的受保护成员
- 派生类无法访问基类的受保护成员
- 继承期间受保护成员的皮条
- 访问说明符(私有/公共/受保护)如何在内部工作(限制成员访问)?
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 是否可以基于私有/受保护成员分配类枚举?
- 依赖模板基础的受保护成员
- 将派生类构造函数参数传递给受保护的成员
- 无法从派生类型的作用域访问另一个实例的受保护成员
- 为什么我无法使用受保护/私有继承访问派生实例中基类的受保护成员?
- 将对象强制转换为派生类以访问父类的受保护成员
- 使从一个基类派生的类能够使用继承的受保护成员
- 无法访问派生类函数内的基类的受保护数据成员
- 派生类如何使用基类的受保护成员