C ++ reinterpret_cast - 这将总是工作正常吗? [英] C++ reinterpret_cast - will this always work correctly?

查看:131
本文介绍了C ++ reinterpret_cast - 这将总是工作正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了MyString和MyStringConst类。现在我需要不时地传递MyString作为MyStringConst,因此重载转换运算符。我写了这个

I have written MyString and MyStringConst class. Now I need from time to time pass MyString as MyStringConst, hence overload cast operator. I have written this

MyString::operator const MyStringConst &() const
{
    return reinterpret_cast<const MyStringConst &>(*this);
}

MyString有此数据

MyString has this data

char * str;
int length;
volatile int hashCode;
int bufferSize;

MyStringConst有此数据

MyStringConst has this data

const char * c_str;
int length;
volatile int hashCode;

另外还有一些方法,两个字符串都可以重新计算hashCode。

Plus there are some methods, that in both strings can recalculate hashCode.

这段代码是否正确写入。我已经在MSVC 2013上测试它,它是工作正常,但我不知道它是否可以在生产代码中使用,可以用不同的编译器编译。

Is this code correctly written. I have tested it on MSVC 2013 and it is working correctly, but I have no idea if it can be used in production code, that can be compiled with different compiler.

推荐答案

数据成员的公共初始序列是不同的,并且C ++不能保证在这种情况下的布局,即使类型只有 const 资格。否则, union s的保证将有效地暗示,如果类型是标准布局类型,则它们需要具有公共布局(根据9.5 [class.union]

The common initial sequence of the data member is different and C++ makes no guarantee at all about the layout in this case, even if the types differ only by const qualification. Otherwise the guarantees for unions would effectively imply that the types need to have a common layout if they are standard-layout types (according to a note in 9.5 [class.union] paragraph 1).

在实践中,我期望这两种类型的布局是相同的,并且 reinterpret_cast 但是没有标准的保证。基于你的注释 MyStringConst 只保存一个指向字符串的指针,即,而不是转换为引用,我只是返回一个适当构造的 MyStringConst 并避免依赖未定义的行为:

In practice I would expect that the two types are laid out identical and that the reinterpret_cast works but there is no guarantee by the standard. Based on your comment MyStringConst merely holds a pointer to the string, i.e., instead of converting to references, I would just return a suitably constructed MyStringConst and avoid relying on undefined behavior:

MyString::operator MyStringConst() const {
    return MyStringConst(str, length);
}

MyString 仍然必须存活,只要转换的结果,但这与使用 reinterpret_cast 的情况没有不同。

The MyString object still has to live as long as the result from the conversion but this is no different to the case using reinterpret_cast.

BTW, hashCode 上的 volatile 是不明智的:唯一的效果是减慢程序。我想你正在尝试使用它来实现线程之间的同步,但在C ++ volatile 不能帮助你:在一个数据竞争当写成员在一个线程也在另一个线程中被不同步地访问。您会拼写成员

BTW, the volatile on the hashCode is ill-advised: the only effect it will have is to slow down the program. I guess you are trying to use it to achieve synchronization between threads but in C++ volatile doesn't help with that at all: you get a data race when writing the member in one thread it is also accessed unsynchronized in another thread. You'd spell the member

std::atomic<int> hashCode;

这篇关于C ++ reinterpret_cast - 这将总是工作正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆