User defined literals must start with an underscore.
This is a more or less universally well-known rule that you can find on every layman-worded site talkin
Yes: the combination of forbidding the use of _
as the start of a global identifier coupled with requiring non-standard UDLs to start with _
means that you can't put them in the global namespace. But you shouldn't be dirtying up the global namespace with stuff, especially UDLs, so that shouldn't be much of a problem.
The traditional idiom, as used by the standard, is to put UDLs in a literals
namespace (and if you have different sets of UDLs, then you put them in different inline namespaces
below that namespace). That literals
namespace is typically underneath your main one. When you want to use a particular set of UDLs, you invoke using namespace my_namespace::literals
or whichever sub-namespace contains your literal set of choice.
This is important because UDLs tend to be heavily abbreviated. The standard for example uses s
for std::string
, but also for std::chrono::duration
of seconds. While they do apply to different kinds of literals (s
applied to a string is a string, while s
applied to a number is a duration), it can sometimes be confusing to read code that uses abbreviated literals. So you shouldn't throw literals at all users of your library; they should opt-in to using them.
By using different namespaces for these (std::literals::string_literals
and std::literals::chrono_literals
), the user can be up-front about which sets of literals they want in which parts of code.