Skip to content

Warning against defining user-defined literals in the global namespace #282

Closed

Description

@Sarcasm

Hello,

I was just giving a small glimpse at the code, and I noticed that some user-defined literals are not part of the rclcpp namespace.

I think it is dangerous, and as a user of the library, I would prefer that the library does not pollute the global namespace.

For example, imagine another library decide to do the same, but using the "Possible implementation" definition of the cppereference.com documentation for operator ""s?

It means we can end up in the following situation:

rclcpp.hpp

...
inline const std::chrono::seconds operator"" _s(unsigned long long s)
{
  return std::chrono::seconds(s);
}
...

foo.hpp

...
constexpr std::chrono::seconds operator ""_s(unsigned long long s)
{
    return std::chrono::seconds(s);
}
...

my_node.cpp

#include <foo.hpp>
#include <rclcpp/rclcpp.hpp>

int main()
{
    std::cout << (30_s).count() << " seconds\n";
    return 0;
}

The code does not compile, live demonstration available here:

In the standard library the chrono literals are in the namespace std::chrono_literals.
By symmetry I guess the rclcpp ones could be defined in rclcpp_literals, but I guess being under the rclcpp namespace makes even more sense, e.g: rclcpp::literals, only one namespace (and sub-namespaces) for the library.

Activity

  1. wjwwood commented on Nov 29, 2016

    @wjwwood
    Member

    Yeah, this was sortof a temporary solution because C++14 was not widely available at the time and the chrono literals were only added in C++14. We should consider removing this in favor of the literals provided by C++14 (http://en.cppreference.com/w/cpp/chrono/operator%22%22s). Now that C++14 is more widely available, we'll likely make it a requirement, unless we run into something that blocks us from doing so.

    I didn't put them in a namespace because I didn't want to clutter the minimal user examples, but you're right that if we keep them, we should do something like put them in a namespace and make the user do this:

    #include <rclcpp/rclcpp.hpp>
    
    using namespace rclcpp::literals;
    
    int main(...) {
      auto timer = node->create_timer(10_s, [](){});
    }
  2. added
    question
    on Nov 29, 2016
  3. Sarcasm commented on Nov 30, 2016

    @Sarcasm
    ContributorAuthor

    Ok, thank you.

    Let me know if a pull request interest you, that would be a good excuse for me to take a look at ros2.

  4. wjwwood commented on Nov 30, 2016

    @wjwwood
    Member

    We're always happy to see a pull request. If you're interested in making one, I'd suggest putting the literals in the rclcpp::literals namespace, perhaps in their own rclcpp/literals.hpp file which would be included in rclcpp/rclcpp.hpp, and then either do using namespace rclcpp::literals in rclcpp/rclcpp.hpp to preserve existing behavior or go and update all of the examples and demos in various repositories that would be affected by this change. If you just move them into a namespace and then do the using namespace to preserve the existing behavior, then we can do this in two steps, one that only touches this repository and then a second which touches all of the code out there using the rclcpp::literals implicitly.

  5. added
    enhancement
    and removed
    question
    on Nov 30, 2016
  6. wjwwood commented on Nov 30, 2016

    @wjwwood
    Member

    This would also be a good opportunity to consider using the constexpr like the "possible implementation" example on the cppreference website.

  7. dirk-thomas commented on Nov 30, 2016

    @dirk-thomas
    Member

    If the C++14 chrono_literals work on all current platforms (Ubuntu Xenial, Mac OS, Windows using VS 2015 Update 3) it would be good to use them.

  8. wjwwood commented on Nov 30, 2016

    @wjwwood
    Member

    That's fine too, but we have to make an explicit decision to require that version of C++, which means making an announcement, updating the wiki pages, and updating the -std statements in all packages. That's probably not something an external contributor could do on their own, but @Sarcasm is welcome to open prs to help get the ball moving on that too, as an alternative to what I described above.

  9. Sarcasm commented on Nov 30, 2016

    @Sarcasm
    ContributorAuthor

    I will take a look at the first proposition for now.

    Regarding the switch to C++14, a quick look seems to indicate that it should be possible for the 3 platforms you listed.
    Apparently the chrono literals are available since GCC 5, Clang 3.4 and MSVC 14

    I have to get more familiar with the CMakeLists.txt of the ros2 packages but I can already imagine a few patches to propose which could ease making the switch without breaking the current state of things.

  10. added
    in review
    on Dec 1, 2016
  11. wjwwood commented on Dec 17, 2016

    @wjwwood
    Member

    @Sarcasm, since we decided to make (and executed) the switch to c++14, I'm going to touch each of your pull requests to use the std::chrono_literals throughout and remove the literals defined in rclcpp.

  12. self-assigned this
    on Dec 17, 2016
  13. removed
    in review
    on Dec 17, 2016
  14. wjwwood commented on Dec 17, 2016

    @wjwwood
    Member

    Thanks for contribution @Sarcasm, sorry for the delay while we sorted out the c++14 stuff.

  15. Sarcasm commented on Dec 17, 2016

    @Sarcasm
    ContributorAuthor

    No problem I understand the decision was an important one, I'm glad you made the switch.
    Thank you for the taking the time to look at the PRs.

  16. added a commit that references this issue on Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions