Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move user-defined literals in their own namespace #284

Merged
merged 4 commits into from
Dec 17, 2016

Conversation

Sarcasm
Copy link
Contributor

@Sarcasm Sarcasm commented Dec 1, 2016

Following the discussion from #282 (comment)

There are 3 commits:

  1. The first one is not supposed to break the examples and tests.
  2. The second one "depollutes" the global namespace, this one require the example and tests to be updated.
    The tests and examples will come in separate pull requests.
  3. The third one, uses a constexpr implementation as seen on cppreference.com

Connects to #282

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

Cool, thanks!

I added the Connects to ros2/rclcpp#282 to the end of each of your pr descriptions to "link" them to the original issue in our issue tracker: https://waffle.io/ros2/ros2

I'll start some CI for you shortly.

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

CI:

  • Linux:
    • Build Status
  • OS X:
    • Build Status
  • Windows:
    • Build Status

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

Apparently one of our linters (cpplint.py) does not like the using namespace directive. I can somewhat understand that, but honestly I think this case should be fine. I guess we can either:

  • disable the warning globally
  • disable the warning at each of the lines
  • update to use the more specific using rclcpp::literals::operator ""_ms or whatever in each case

I'd vote for the last, since it's unlikely to result in needing multiple lines in the examples and avoids doing nasty things like disabling linter warnings.

I would prefer to merge this contribution in some form, even if we're thinking about converting to the chrono literals with C++14.

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

@Sarcasm note that if you're interested in reproducing these linter errors, you can just run ament_cpplint path/to/directory/to/lint.

@Sarcasm
Copy link
Contributor Author

Sarcasm commented Dec 1, 2016

Arf, I passed the linters on rclcpp itself but not on the other changes.

I agree it's sad to disable the warning globally.
But I think I prefer a nolint comment than using the full using declaration because IMHO that's how the user-defined literals namespace are supposed to be used.
The using-declaration is difficult to come up from the top of the head IMHO. Making the examples actually more difficult to grasp.
Because it looks too complex, I no longer see the benefits of the literal for some examples, that uses the literal only once.

using rclcpp::literals::operator ""_ms;

void f() {
   spin_some(30_ms);
}

VS

void f() {
   spin_some(std::chrono::milliseconds(30));
}

What do you think of whitelisting literals and *_literals namespace in cpplint?
I can try that, it would be reasonable IMHO.

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

Seeing as we'll need a similar using namespace std::chrono::literals, it makes sense to me to try and make an exception in the cpplint.py checking for this kind of statement. What do others think?

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

@Sarcasm a no lint comment would be ok for me, since I think it will be temporary because we're going to probably do the switch to C++14 soon.

We may or may not have need of the "using namespace .*literals;" exclusion in cpplint.py once we start using std::chrono's literals.

@Sarcasm
Copy link
Contributor Author

Sarcasm commented Dec 1, 2016

Ok, I can do that.
I already implemented the exclusion in cpplint.py, should I propose a PR or is it better to leave this alone?

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

Up to you, worst case we decline the cpplint pr.

@Sarcasm
Copy link
Contributor Author

Sarcasm commented Dec 1, 2016

I updated all the PRs with no lint comments.

And the cpplint proposal is here:

@dirk-thomas
Copy link
Member

I would still avoid using namespace directives in headers since they are a side effect which should be avoided. It comes to a surprise to the user and that is why the general recommendation is not to do it.

Imo the using declaration is preferred. If a file uses the literal only once then it is up to the developer to choose if the using declaration is worth it or to use the full symbol instead.

I also think that we should avoid adding further patches into our cpplint fork. I would rather aim to use upstream at some point in the future and this detaches us even further.

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

I would still avoid using namespace directives in headers since they are a side effect which should be avoided. It comes to a surprise to the user and that is why the general recommendation is not to do it.

I do not believe that was suggested here. Except in the intermediate commit which restored existing behavior. That was removed in the last commit.

Imo the using declaration is preferred. If a file uses the literal only once then it is up to the developer to choose if the using declaration is worth it or to use the full symbol instead.

I agree that the using statement is preferable in pretty much all cases, i.e. using std::string; using std::vector; over using namespace std;, but in the case of the using namespace std::chrono::literals I think it's ok because the operators are inconvenience to specify using the stricter using ... statement and because the literal suffixes defined therein cannot collide with user code by design. See:

http://en.cppreference.com/w/cpp/language/user_literal

ud-suffix - an identifier, introduced by a literal operator or a literal operator template declaration (see below). All ud-suffixes introduced by a program must begin with the underscore character _. The standard library ud-suffixes do not begin with underscores.

So in this very narrow case, I think it's ok to use the using namespace directive.

I also think that we should avoid adding further patches into our cpplint fork. I would rather aim to use upstream at some point in the future and this detaches us even further.

I agree with that. The correct thing to do would be to propose an upstream patch which gives us the option to ignore this linter error if we don't agree with it in every case, or a way to configure cpplint.py to allow the exact line using namespace std::chrono::literals; despite it being a using namespace statement.

@wjwwood
Copy link
Member

wjwwood commented Dec 1, 2016

Even the cppreference website's documentation suggests that you use the using namespace directive (in one of a few ways) to get access to the literal suffixes:

http://en.cppreference.com/w/cpp/chrono/operator%22%22h

These operators are declared in the namespace std::literals::chrono_literals, where both literals and chrono_literals are inline namespaces. Access to these operators can be gained with using namespace std::literals, using namespace std::chrono_literals, and using namespace std::literals::chrono_literals.
In addition, within the namespace std::chrono, the directive using namespace literals::chrono_literals; is provided by the standard library, so that if a programmer uses using namespace std::chrono; to gain access to the duration classes, the duration literal operators become visible as well.

Though they do limit the scope of the using namespace statement to the main function in their examples, which is something we can do. It would also be another way that we could propose cpplint be more flexible: allow these statements in confined scopes. In fact it might already do that, but I'm not sure.

@Sarcasm
Copy link
Contributor Author

Sarcasm commented Dec 1, 2016

I would still avoid using namespace directives in headers since they are a side effect which should be avoided. It comes to a surprise to the user and that is why the general recommendation is not to do it.

You are totally right I can fix my cpplint.py PR to disable the additional checks in headers but if your goal is to use upstream at one point, I think it's best to close the PR. Maybe I can propose upstream.

I checked the code a bit more.
There are 2 situations right now:

  1. examples and demos: have usually 1 use of a literal per file
  2. system_tests: can have a bunch bunch

For the examples and demos, I think the best is to keep them as simple as possible meaning the NOLINT comment would be better avoided and (personal opinion here) the using directive should also be avoided.
If cpplint.py is not a good option then I think it's reasonable to just use things like std::chrono::milliseconds(...).

For system_tests, the NOLINT comment seems reasonable.

Regarding the scope of the using namespace, I can refine the scope but I think in system_tests it will look better for the whole file.

@Sarcasm
Copy link
Contributor Author

Sarcasm commented Dec 2, 2016

For the record, Google would not accept the upstream change:

We (internally) had a long discussion recently and came to the conclusion that using directives are never ever ever ever safe for use in a codebase of any significant size - we cannot endorse this, even just once. > Sorry.

(I'm working on finding a proper forum to share some of the Google-internal guidance, hopefully early next year.)

-- google/styleguide#204 (comment)

@wjwwood
Copy link
Member

wjwwood commented Dec 17, 2016

I've updated all of these pr's to use C++14 and remove references to the rclcpp literals we had before. Looking for reviews and here's CI:

  • Linux:
    • Build Status
  • OS X:
    • Build Status
  • Windows:
    • Build Status

@wjwwood
Copy link
Member

wjwwood commented Dec 17, 2016

Doh, I thought I could get away with the old gist I used before. Here's new CI with an updated repos file:

  • Linux:
    • Build Status
  • OS X:
    • Build Status
  • Windows:
    • Build Status

Sarcasm and others added 4 commits December 16, 2016 18:37
The examples have been migrated to the new namespace.
_ms returned nanoseconds instead of milliseconds.

A few return types where using integral return type
for floating point literals.
@wjwwood
Copy link
Member

wjwwood commented Dec 17, 2016

I had to rebase this one to fix the CI.

@wjwwood
Copy link
Member

wjwwood commented Dec 17, 2016

Ok, finally working. The only remaining warning is in the tutorial package, the allocator example, which we're excluding also, I don't know how to fix it right off but it's related to allocators and c++14 and the new "sized delete". CI:

  • Linux:
    • Build Status
  • OS X:
    • Build Status
  • Windows:
    • Build Status

@wjwwood
Copy link
Member

wjwwood commented Dec 17, 2016

I'm going to merge these because they are blocking the other prs and the changes are relatively straightforward. If anyone has some issues or comments I can address them in a follow up pr.

@wjwwood wjwwood merged commit d00a441 into ros2:master Dec 17, 2016
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Dec 17, 2016
@dirk-thomas
Copy link
Member

@wjwwood This broke the packaging job / ros1_bridge.

@wjwwood
Copy link
Member

wjwwood commented Dec 18, 2016

@dirk-thomas sorry about that, fix is in: ros2/ros1_bridge#52

@dirk-thomas
Copy link
Member

👍

nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
* Clock has multiple time jump callbacks

Replaces pre_update and post_update
Adds callbacks when ROS time is activated or deactivated
Add rcl_clock_change_t, rcl_time_jump_t, rcl_jump_threshold_t
Add rcl_clock_add_jump_callback()
Add rcl_clock_remove_jump_callback()

* Fini generic clock after confirming correct clock type

* test_time no ASAN detections
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
* Close storage before compression
* Only compress if the bag file exists and has content

Signed-off-by: Anas Abou Allaban <allabana@amazon.com>
Signed-off-by: Zachary Michaels <zmichaels11@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants