size_t 到 unsigned int (来自 API 函数)
我正在使用 Oracle API 访问数据库,该 API 有一个函数 readBuffer(char * buffer, unsigned int size); ,我无法对其进行任何更改。
我有一个使用此 API 的类,并且我的函数的签名当前采用 std::string
和 unsigned int
作为大小,问题是当我通过std::string.size()
到我的函数的 size 参数,我从编译器收到警告,从 size_t
转换为 unsigned int
可能会导致数据丢失。
我想知道是否有有效的方法将size_t
转换为unsigned int
,这样我就可以将它传递给我的API而不会收到警告来自编译器?
我理解 size_t 的目的,并且在 google 上搜索此转换会出现很多结果,显示“更改函数以采用 size_t arg”,但在这种情况下我无法更改 API 的签名。
有什么建议吗?
I am using the Oracle API to access a database and this API has a function readBuffer(char * buffer, unsigned int size);
to which I cannot make any changes.
I have a class that uses this API and the signature of my function currently takes a std::string
and an unsigned int
for the size, the problem is that when I pass std::string.size()
to the size argument of my function, I get a warning from my compiler that converting from size_t
to unsigned int
could cause data loss.
I wondered if there is a valid way to convert the size_t
to an unsigned int
so I can pass it to my API and not get a warning from the compiler?
I understand the purpose of size_t and searching google for this conversion turns up a lot of results that say "change the function to take a size_t arg" but I CANNOT change the signature of my API in this case.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,编写一个辅助函数来检查此类转换是否有效,否则抛出异常。像这样的东西:
Yes, write a helper function that will check whether such conversion is valid and throw an exception otherwise. Something like:
好吧,执行一个
static_cast(mystring.size())
。原因是 std::size_t 通常是指针大小,但在 64 位平台上,int 仍然是 32 位。在这种情况下,数据丢失的唯一原因是相关字符串的长度超过 2^32 字节。
如果您知道这种情况不会发生,请在某处放置一个
assert
来捕获这种情况,并将编译器static_cast
置于静默状态。Well, do a
static_cast<unsigned int>(mystring.size())
.The reason is that
std::size_t
is usually pointer-size, but there are 64 bit platforms on which anint
is still 32 bits. In this case, the only reason for data loss would be if the string in question had a length of more than 2^32 bytes.If you know that this won't happen, put an
assert
somewhere to catch this case andstatic_cast
the compiler to silence.如果你想变得偏执:
If you want to be paranoid:
您可以使用构造强制转换
。当然,正确的方法是 API 接受
size_t
...You can force the conversion using the
construct. Of course the correct way would be for the API to accept
size_t
...这里的风险是
size_t
可能大于 (unsigned
)int
,因此在这种情况下您无法安全地进行转换。例如,可以想象
int
是 32 位,而size_t
是 64 位。我不知道这样的系统/配置在我的脑海中,但它可能会发生。对于大多数“合理”的系统,两者都至少为 32 位,并且单个 4 GB 字符串仍然(也许)不太可能出现。
因此,您可以直接进行强制转换,这将是有效的,但对于所有可能的系统和极端情况来说,它并不“安全”。
The risk here is that
size_t
might be larger than (unsigned
)int
, and thus you cannot safely convert if that is the case.For instance, it's conceivable that
int
is 32 bits, whilesize_t
is 64 bits. I don't know such a system/configuration off the top of my head, but it could occur.For most "reasonable" systems, both will be at least 32 bits, and a single 4 GB string is still (perhaps) unlikely to occur.
So then you could just cast, that would be valid but it would not be "safe" for all possible systems and corner cases.