Skip to content

C++ domain: support numeric user-defined literals (UDLs) in expressions #97

@rowan-stein

Description

@rowan-stein

C++ User Defined Literals not supported

The code as below

namespace units::si {

inline constexpr auto planck_constant = 6.62607015e-34q_J * 1q_s;

}

causes the following error:

WARNING: Invalid definition: Expected end of definition. [error at 58]
  constexpr auto units::si::planck_constant = 6.62607015e-34q_J * 1q_s
  ----------------------------------------------------------^

According to https://github.com/sphinx-doc/sphinx/blob/3.x/sphinx/domains/cpp.py#L4770 Sphinx seems to not have features for UDLs. Could you please add those?

Observed failure and reproduction steps

  • Minimal repro in a .rst file:

    .. cpp:namespace:: units::si
    
    .. cpp:var:: inline constexpr auto planck_constant = 6.62607015e-34q_J * 1q_s
  • Build with warnings as errors:

    sphinx-build -b html . _build -W
  • Expected current behavior: warning shown above. This is due to the C++ domain parser accepting the floating literal 6.62607015e-34 but not the adjacent UDL suffix q_J, leaving trailing text and triggering "Expected end of definition".

Proposed resolution (summary)

  • Extend the C++ domain literal parsing to accept an optional user-defined literal suffix (identifier: [A-Za-z_][A-Za-z0-9_]*) immediately following numeric literals, alongside existing builtin suffixes (u/U/l/L/f/F). Builtin suffixes and UDLs should not be combined on the same literal.
  • Add tests covering positive and negative cases for numeric UDLs.

This issue tracks adding numeric UDL support so that the example above parses without warnings.

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