【发布时间】:2015-03-25 10:28:15
【问题描述】:
问题与this post有关。
部分权威用户表示,以下代码破坏strict aliasing rules。
#include <boost/static_assert.hpp>
template <typename T>
struct MyType {
private:
T data;
public:
template <typename U>
operator U () {
BOOST_STATIC_ASSERT_MSG(sizeof(U) == sizeof(T),"Trying to convert to data type of different size");
return *((U*) &data);
}
template <typename U>
NeonVectorType<T>& operator =(const U& in) {
BOOST_STATIC_ASSERT_MSG(sizeof(U) == sizeof(T),"Trying to copy from data type of different size");
data = *((T*) &in);
return *this;
}
}
但是,我从不使用指向 write 数据的指针,也从不共享指向它的指针,因此我无法看到变量中包含的值如何在编译器没有意识到这一点的情况下发生变化正在发生。我的印象是,也许我违反了一些规则,但不是严格的别名规则……
注意:我不知道这有多重要,但我的编译器 (gcc 4.9) 不会发出警告。
【问题讨论】:
-
我怀疑您将严格的别名规则与指针别名混淆了,指针别名是别名的另一种形式。
标签: c++ pointers c++98 strict-aliasing