C ++对齐的未来:通过价值传递? [英] The future of C++ alignment: passing by value?

查看:99
本文介绍了C ++对齐的未来:通过价值传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Eigen库文档时,我注意到某些对象无法通过值传递 . C ++ 11中是否有任何开发或计划中的开发可以使按值传递此类对象安全?

Reading the Eigen library documentation, I noticed that some objects cannot be passed by value. Are there any developments in C++11 or planned developments that will make it safe to pass such objects by value?

此外,为什么按值返回此类对象没有问题?

Also, why is there no problem with returning such objects by value?

推荐答案

他们可以在C ++ 11中做到这一点:

They could do this in C++11:

class alignas(16) Matrix4f
{
    // ...
};

现在,该类将始终在16字节的边界上对齐.

Now the class will always be aligned on a 16-byte boundary.

此外,也许我很傻,但这绝对不是问题.给定这样的课程:

Also, maybe I'm being silly but this shouldn't be an issue anyway. Given a class like this:

class Matrix4f
{
public:
    // ...
private:
    // their data type (aligned however they decided in that library):
    aligned_data_type data;

    // or in C++11
    alignas(16) float data[16];
};

编译器现在不得不在16字节边界上分配一个Matrix4f,因为那样会破坏它.类级别alignas应该是冗余的.但是过去,以某种方式知道我是错的.

Compilers are now obligated to allocate a Matrix4f on a 16-byte boundary anyway, because that would break it; the class-level alignas should be redundant. But I've been known to be wrong in the past, somehow.

这篇关于C ++对齐的未来:通过价值传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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