简体   繁体   English

将 size_t* 转换为 hsize_t*

[英]casting size_t* to hsize_t*

On my platform, size_t and HDF5's hsize_t are both unsigned 64bit integers, yet they are different C++ types (one is unsigned long , the other unsigned long long ) and cannot be used interchangibly.在我的平台上, size_t和 HDF5 的hsize_t都是无符号 64 位整数,但它们是不同的 C++ 类型(一个是unsigned long ,另一个是unsigned long long )并且不能互换使用。

 using hsize_t = unsigned long long;
 void library_function(const hsize_t*);

 void client_code(std::vector<size_t> const&input)
 {
     library_function(input.data());     // error
 }

So my question: Is it in this situation required to convert所以我的问题是:在这种情况下是否需要转换

 void client_code(std::vector<size_t> const&input)
 {
     std::vector<hsize_t> tmp(input.size());
     std::transform(input.begin(), input.end(), tmp.begin(),
                    [](size_t x) { return hsize_t(x); });
     library_function(tmp.data());
 }

or can I simply cast?或者我可以简单地投射?

 void client_code(std::vector<size_t> const&input)
 {
     static_assert(sizeof(size_t)==sizeof(hsize_t),"!!");
     library_function(reinterpret_cast<const hsize_t*>(input.data()));     // correct?
 }

(this compiles, but is it guaranteed to work correctly?) (这可以编译,但能保证正常工作吗?)

Accessing the value through the resulting pointer of the reinterpret_cast is an aliasing violation if the two types are indeed, up to cv-qualification, different non- char integer types that are not unsigned / signed versions of one another.如果这两种类型确实是(达到 cv 限定的)不同的非char整数类型,并且它们不是彼此的unsigned /有signed版本,则通过reinterpret_cast的结果指针访问该值是一种别名违规。 Since you said that the two types here are unsigned long and unsigned long long , this applies to your case.由于您说这里的两种类型是unsigned longunsigned long long ,因此这适用于您的情况。

You cannot use it if you want to stay in the bounds of what the standard defines.如果您想停留在标准定义的范围内,则不能使用它。 There is also no way to type-pun one integer type to another, so you always need to copy (that may or may not be optimized out by the compiler).也无法将一种整数类型键入另一种类型,因此您始终需要复制(编译器可能会或可能不会对其进行优化)。


You can usually still make this work in practice (even though it is undefined behavior according to the standard) by either setting some compiler flag like -fno-strict-aliasing which instructs the compiler to not optimize based on the guarantees given to it by the aliasing rule (which may come with worse performance) or by "hiding" the violating pointer access from the optimizer by putting it in a different translation unit (if link-time-optimization is not used).您通常仍然可以通过设置一些编译器标志(如-fno-strict-aliasing指示编译器不要根据由别名规则(可能会带来更差的性能)或通过将违规指针访问放在不同的翻译单元中(如果未使用链接时间优化)来“隐藏”来自优化器的违规指针访问。 See also @HolyBlackCat's comment on the question.另请参阅@HolyBlackCat 对该问题的评论。

Either is going to be unportable though.两者都将是不可移植的。 Eg a different compiler (version) might do link-time optimization by default or not support the -fno-strict-aliasing or similar.例如,不同的编译器(版本)可能会默认进行链接时优化或不支持-fno-strict-aliasing或类似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM