clang-format for Eigen matrix initialization
Asked Answered
A

2

9

To inialize for example Eigen::Matrix3i we can use syntax:

Eigen::Matrix3i T;
T << 1, 0, 0,
     0, 2, 0,
     0, 0, 3;

However, when using clang-format (3.6 in my case) with Google style this nice initialization turns into:

Eigen::Matrix3i T;
T << 1, 0, 0, 0, 2, 0, 0, 0, 3;

Is there an easy way to avoid this? Is there a way to tell clang-format to skip something like this?

Arianna answered 13/4, 2016 at 10:24 Comment(0)
E
7

It looks like your only option is to use a rather ugly clang-format switching syntax:

Eigen::Matrix3i T;
// clang-format off
T << 1, 0, 0,
     0, 2, 0,
     0, 0, 3;
// clang-format on
Extract answered 13/4, 2016 at 11:7 Comment(3)
Uglifying the code so the prettifier doesn't uglify it seems to be sort of invalidating the point though.Albedo
Ugh. Well, this is a solution, but yeah, I agree with @Albedo here. I will leave the question open for some time in case somebody comes up with something else. If this does not happen, I will accept this as an answer.Arianna
@Albedo yeah, I agree too, but this solution could be viable if you're forced to use two coding styles(e.g. for checkout and commits) and in similar situationsExtract
L
3

Did you try this?

Eigen::Matrix3i T;
T << 1, 0, 0, //
     0, 2, 0, //
     0, 0, 3; //
Lyonnaise answered 3/5, 2022 at 7:45 Comment(2)
Same answer as others.Perdure
What do you mean? There is only one other answer and it's different than this.Whinchat

© 2022 - 2024 — McMap. All rights reserved.