C ++特征根初始化静态矩阵 [英] C++ Eigen initialize static matrix

查看:77
本文介绍了C ++特征根初始化静态矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在头文件中初始化静态特征矩阵?我想将其用作全局变量。

Is it possible to initialize a static eigen matrix4d in a header file? I want to use it as a global variable.

我想做以下事情:

static Eigen::Matrix4d foo = Eigen::Matrix4d(1, 2 ... 16);

或类似于矢量:

static Eigen::Matrix4d foo = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; 

这里是链接到本征矩阵文档。

Here is a link to the eigen matrix docs. I can't seem to find how to do this from there.

推荐答案

关于Dawid的回答(其中有一个很小的地方),我似乎找不到从那里开始的方法。问题,请参阅评论),您可以执行以下操作:

On the lines of Dawid's answer (which has a small issue, see the comments), you can do:

static Eigen::Matrix4d foo = [] {
    Eigen::Matrix4d tmp;
    tmp << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16;
    return tmp;
}();

返回值优化会临时处理,因此不必担心多余的副本。

Return value optimization takes care of the temporary, so no worries about an extra copy.

这篇关于C ++特征根初始化静态矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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