boost中max_align的实现疑问

本文分析了Boost库中max_align实现存在的问题,特别是在处理含有虚基类的成员函数指针时,对齐边界计算不准确的情况。通过具体示例说明了现有实现的局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在查看boost::aligned_storage的实现代码时,看到,其中的两个枚举量:
size和alignment,size是传入类型的sizeof, alignment则是其对齐边界。
alignment = (
              alignment_ == std::size_t(-1)
            ? ::boost::detail::aligned_storage::alignment_of_max_align
            : alignment_
            )
默认情况下alignment_参数为-1,那么就来分析一下::boost::detail::aligned_storage::alignment_of_max_align。
其中只有一句:
alignment_of_max_align = ::boost::alignment_of<max_align>::value
alignment_of的定义很明确了,就是类型T的对齐边界。max_align意味这想透过此获得系统最大的对齐边界。
然而,max_align的实现似乎有问题。
在分析之前,我们看看一段测试代码,在vc7.1环境下,
cout << alignment_of<detail::max_align>::value << endl;
cout << aligned_storage<sizeof(int)>::alignment << endl;
输出
8
8
似乎系统的最大对齐边界就是8.我们在看max_align的实现:
union max_align
{
    BOOST_PP_LIST_FOR_EACH_I(
          BOOST_TT_CHOOSE_T
        , ignored
        , BOOST_TT_ALIGNMENT_TYPES
        )
};
嗯,再去看关键宏定义的序列:
#define BOOST_TT_ALIGNMENT_TYPES                                /
        BOOST_PP_LIST_APPEND(BOOST_TT_ALIGNMENT_BASE_TYPES,     /
                             BOOST_TT_ALIGNMENT_STRUCT_TYPES)

#define BOOST_TT_ALIGNMENT_BASE_TYPES BOOST_PP_TUPLE_TO_LIST( /
        11, ( /
        char, short, int, long, float, double, long double /
        , void*, function_ptr, member_ptr, member_function_ptr))
在看function_ptr, member_ptr, member_function_ptr是如何定义的:
class alignment_dummy;
typedef void (*function_ptr)();
typedef int (alignment_dummy::*member_ptr);
typedef int (alignment_dummy::*member_function_ptr)();
好了,我们总结一下,max_align是上述的11种数据类型的union,这个union的大小在我们的测试中(VC7.1),为8.然而有一种情况被忽略了:
class A{};
class B : virtual A{};
cout << alignment_of<void (B::*)(int) >::value << endl;
输出是多少?
12
很显然,member_function_ptr并不能涵盖所有的成员函数指针,对于存在有虚基类的情况下,是不适合的。
那么,这个max_align的实现,算不算有问题呢?到目前为止,我还没有能够清理出实现代码,那些扩展宏实在恐怖,看得我头晕目眩。所以,我的疑问还只是疑问。

void PointProcessing::coarseRegistration( std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr>& clusters, // 多簇源点云 pcl::PointCloud<pcl::PointXYZ>::Ptr& target, // 目标点云 std::vector<Eigen::Matrix4f>& transformation_matrices, // 输出变换矩阵集合 float sac_ia_max_distance, // SAC-IA最大对应距离 int feature_radius // FPFH特征半径 ) { transformation_matrices.clear(); // 遍历每个簇 for (const auto& cluster : clusters) { // --- 1. 源簇关键点提取 --- pcl::PointCloud<pcl::PointXYZ>::Ptr src_keypoints(new pcl::PointCloud<pcl::PointXYZ>); pcl::UniformSampling<pcl::PointXYZ> uniform_sampling; uniform_sampling.setInputCloud(cluster); uniform_sampling.setRadiusSearch(2.0); // 关键点采样半径(可参数化) uniform_sampling.filter(*src_keypoints); // --- 2. 目标关键点提取(复用同一采样器)--- pcl::PointCloud<pcl::PointXYZ>::Ptr tgt_keypoints(new pcl::PointCloud<pcl::PointXYZ>); uniform_sampling.setInputCloud(target); uniform_sampling.filter(*tgt_keypoints); // --- 3. 使用自定义函数计算法线 --- pcl::PointCloud<pcl::Normal>::Ptr src_normals(new pcl::PointCloud<pcl::Normal>); computernormals(src_keypoints, src_normals, 5.0); // 法线半径5.0 pcl::PointCloud<pcl::Normal>::Ptr tgt_normals(new pcl::PointCloud<pcl::Normal>); computernormals(tgt_keypoints, tgt_normals, 5.0); // --- 4. 使用自定义函数计算FPFH特征 --- pcl::PointCloud<pcl::FPFHSignature33>::Ptr src_features(new pcl::FPFHSignature33); computerFPFH(src_keypoints, src_normals, src_features, feature_radius); pcl::PointCloud<pcl::FPFHSignature33>::Ptr tgt_features(new pcl::FPFHSignature33); computerFPFH(tgt_keypoints, tgt_normals, tgt_features, feature_radius); // --- 5. SAC-IA配准 --- pcl::SampleConsensusInitialAlignment<pcl::PointXYZ, pcl::PointXYZ, pcl::FPFHSignature33> sac_ia; sac_ia.setInputSource(src_keypoints); sac_ia.setSourceFeatures(src_features); sac_ia.setInputTarget(tgt_keypoints); sac_ia.setTargetFeatures(tgt_features); sac_ia.setMaxCorrespondenceDistance(sac_ia_max_distance); pcl::PointCloud<pcl::PointXYZ>::Ptr aligned(new pcl::PointCloud<pcl::PointXYZ>); sac_ia.align(*aligned); // 无返回值,直接执行 // 检查变换矩阵有效性 Eigen::Matrix4f matrix = sac_ia.getFinalTransformation(); if (!matrix.isZero() && !matrix.hasNaN()) { transformation_matrices.push_back(matrix); } else { transformation_matrices.push_back(Eigen::Matrix4f::Identity()); } } } 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C2440 “初始化”: 无法从“Y *”转换为“pcl::PointCloud<pcl::FPFHSignature33> *” PCL_Debug d:\program files\pcl 1.8.1\3rdparty\boost\include\boost-1_64\boost\smart_ptr\shared_ptr.hpp 362 错误 C2439 “boost::shared_ptr<pcl::PointCloud<pcl::FPFHSignature33>>::px”: 未能初始化成员 PCL_Debug d:\program files\pcl 1.8.1\3rdparty\boost\include\boost-1_64\boost\smart_ptr\shared_ptr.hpp 362
03-08
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值