一个指针可以存储在std :: mbstate_t类型中吗? [英] Can a pointer be stored in std::mbstate_t type?

查看:381
本文介绍了一个指针可以存储在std :: mbstate_t类型中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用iconv的std :: codecvt facet的实现。 实施在std :: mbstate_t状态参数中存储指向堆分配数据的指针。

I'm writing an implementation of std::codecvt facet that uses iconv. This implementation stores a pointer to heap-allocated data in std::mbstate_t state argument.

一切正常,但是这个代码64位兼容吗?
是否有一个指针大小超过std :: mbstate_t大小的平台?

Everything works fine, but is this code 64-bit compatible? Is there a platform where a pointer size exceeds the size of std::mbstate_t?

推荐答案

codecvt 模板采用状态类型作为参数?你可以只使用指针类型吗?我不记得使用 codecvt 的各种类是否对状态类型有要求。

Doesn't the codecvt template take the state type as a parameter? Can you just use a pointer type there instead? I can't remember whether the various classes that use a codecvt place requirements on the state type.

假设你不能只是改变状态类型...在MSVC 2008上, mbstate_t typedef d作为 int 。该标准只需要 int 大于16位,不大于长,因此它不是64位安全。我想你需要将索引或键存储到某些数据结构中,而不是指针。

Assuming that you can't just change the state type... on MSVC 2008, mbstate_t is typedefd as an int. The standard only requires that int be larger than 16 bits and no larger than a long, so it's not 64-bit safe. I guess you would need to store an index or key into some data structure instead of a pointer.

更新:

以下编译在VS2008下,至少:

The following compiles under VS2008, at least:

std::wstring const in = L"input";
size_t const buf_size = 256;
char* buf = new char[buf_size];
wchar_t const* char_next;
char * byte_next;
void* state = NULL;

typedef std::codecvt<wchar_t, char, void*> codecvt_t;
codecvt_t::result res =
    std::use_facet<codecvt_t>(std::locale()).out(
    	state, in.c_str(), in.c_str() + in.length(),
    	char_next, &buf[0], &buf[buf_size], byte_next);

这篇关于一个指针可以存储在std :: mbstate_t类型中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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