Skip to content

Remove std::endl from print functions #2018

@p-zach

Description

@p-zach

Issue

Currently, classes append std::endl in their print statements, such as Pose2:

void Pose2::print(const string& s) const {
  std::cout << (s.empty() ? s : s + " ") << *this << std::endl;
}

It seems to me that this is unnecessary and goes against best practice. It means that users have less control over how they can format their print statements. For example, I would have liked the Python code

print(f"{global_point} transformed by {origin} into local space -> {transformed}")` 

to yield

[5. 5.] transformed by (1, 1, 3.14159) into local space -> [-4. -4.]

but instead it gives the following:

Image

This example also demonstrates best practice as followed by Numpy, which does not automatically insert newlines.

Solution

Remove std::endl from print functions. Perhaps it can be implemented so that the endline is only added if the user has not supplied a prefix string. I can create a PR if this solution is desired.

Caveats

This is an opinionated issue, but I think that allowing the user to format their strings how they wish is strictly better than the current approach. The optional string prefix is handy for debugging and writing quick statements, but for even slightly more elaborate formatting it gets in the way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions