本文介绍了运营商:alignof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在给定系统上,一种类型的对齐 要求可能并且允许小于该类型的大小。例如, 这里是一个假设的设置: int是4个字节,但只需要在2字节边界上对齐。 /> 如果我们有某种对齐的话操作员,然后我们就可以做下面的 : int main() { int array [2]; int * p = array; / *以下函数在别处定义。我在这里使用 来使这个片段尽可能简单。 * / AdvancePointerByBytes(p,alignof(int)); * p = 5; } 在comp.lang.c上,Hallvard B Furuseth设计了这样一个操作员,并且很多人都使用它。 我已经将它改编为C ++,我编写了一个示例程序,包含 两个头文件和一个源文件。有什么想法吗? / * bEGINNING OF advance_ptr.hpp * / #ifndef INCLUDE_ADVANCE_PTR_HPP #define INCLUDE_ADVANCE_PTR_HPP #include< cstddef> 模板< class T> inline void AdvancePtr(T *& p ,std :: size_t const bytes) { p = reinterpret_cast< T *>( const_cast< unsigned char *>( reinterpret_cast< const unsigned char *>(p)+ bytes ) ); } 模板< class T> inline void RetreatPtr(T *& p,std :: size_t const bytes) { p = reinterpret_cast< T *>( const_cast< unsigned char *>( reinterpret_cast< const unsigned char *>(p) - bytes ) ); } #endif / * END OF advance_ptr.hpp * / / *开始对齐alignof.hpp * / #ifndef INCLUDE_ALIGNOF_HPP #define INCLUDE_ALIGNOF_HPP #include< cstddef> 模板< class T> struct StructForAlign { unsigned char first_byte; T obj; }; #define alignof (type)(offsetof(StructForAlign< type>,obj)) #endif / * END OF alignof.hpp * / / *开始使用main.cpp * / #include< iostream> #include" advance_ptr.hpp" ; #include" alignof.hpp" int main() { 长双数组[2]; long double * p = array; AdvancePtr(p,alignof(long double)); * p = 23235.2352; std :: cout<< * p<< ''\ n''; } / * END OF main.cpp * / - Frederick GothamIt is possible and permissible on a given system, that the alignmentrequirements of a type are less than the size of that type. For instance,here''s a hypothetical set-up:int is 4 bytes, but need only be aligned on a 2 byte boundary.If we had some sort of "alignof" operator, then we would be able to dothe following:int main(){int array[2];int *p = array;/* The following function is defined elsewhere. I use ithere to make this snippet as simple as possible. */AdvancePointerByBytes( p, alignof(int) );*p = 5;}Over on comp.lang.c, Hallvard B Furuseth devised such an operator, and alot of people use it.I''ve adapted it to C++, and I''ve written a sample program consisting oftwo headers and one source file. Any thoughts on it?/* BEGINNING OF advance_ptr.hpp */#ifndef INCLUDE_ADVANCE_PTR_HPP#define INCLUDE_ADVANCE_PTR_HPP#include <cstddef>template<class T>inline void AdvancePtr( T * &p, std::size_t const bytes ){p = reinterpret_cast< T* >(const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(p) + bytes));}template<class T>inline void RetreatPtr( T * &p, std::size_t const bytes ){p = reinterpret_cast< T* >(const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(p) - bytes));}#endif/* END OF advance_ptr.hpp *//* BEGINNING OF alignof.hpp */#ifndef INCLUDE_ALIGNOF_HPP#define INCLUDE_ALIGNOF_HPP#include <cstddef>template<class T>struct StructForAlign {unsigned char first_byte;T obj;};#define alignof(type) ( offsetof( StructForAlign< type >, obj ) )#endif/* END OF alignof.hpp *//* BEGINNING OF main.cpp */#include <iostream>#include "advance_ptr.hpp"#include "alignof.hpp"int main(){long double array[2];long double *p = array;AdvancePtr( p, alignof(long double) );*p = 23235.2352;std::cout << *p << ''\n'';}/* END OF main.cpp */--Frederick Gotham推荐答案 我不明白你想要做什么。假设你 有这样一个运算符它将用于什么?I don''t understand what you are trying to do. Presuming youhave such an operator what would it be used for? 这是目前在C ++ 0X工作草案中: http://www.open-std.org/jtc1/ sc22 / wg ... 2006 / n2009.pdf 搜索alignment_of。 在此期间,解决方法如:你的,boost :: alignment_of和 std :: tr1 :: alignment_of可用。 -HowardThis is currently in the C++0X working draft: http://www.open-std.org/jtc1/sc22/wg...2006/n2009.pdfSearch for alignment_of.In the meantime, workarounds such as yours, boost::alignment_of, andstd::tr1::alignment_of are available.-Howard 这是IMO混合不兼容的抽象级别。对齐是 低级功能的一部分,你并不真正关心指针最终指向的类型为什么的类型。它是IMO Ungood和几乎Evil(TM)提供客户代码简单这个低级别的东西与更高级别的代码混合起来的功能。 虽然我没有使用它,但基于一般理由我建议你带一个 看看引用其他线程的Boost实用程序。 - 答:因为它弄乱了订单人们通常会阅读文字。 问:为什么这么糟糕? A:热门帖子。 问:什么是usenet和电子邮件中最烦人的事情是什么?This is IMO mixing incompatible levels of abstraction. Alignment ispart of low-level functionality where you don''t really care about thetype T that the pointer will finally end up pointing to. It''s IMOUngood and almost Evil(TM) to offer client code "easy" functionality tomix such low-level things with higher level code.Although I haven''t used it, on general grounds I recommend you take alook at the Boost utility referenced else-thread.--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail? 这篇关于运营商:alignof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 15:43