Skip to content

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

@Sarcasm

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.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions