AutosarC++17_10-A13.1.1ΒΆ

User-defined literals shall not be used

Required inputs: IR

Rationale

User-defined literals permits only following types for parameter lists:

  • const char*
  • unsigned long long int
  • long double
  • char
  • wchar_t
  • char16_t
  • char32_t
  • const char*, std::size_t
  • const wchar_t*, std::size_t
  • const char16_t*, std::size_t
  • const char32_t*, std::size_t

A programmer has limited control on the types of parameters passed to user-definedconversion operators. Also, it is implementation-defined whether fixed-size types from<cstdint> header are compatible with the types allowed by user-defined literals.

Note that user-defined literals are not widespread in C++ Standard Library. They areused to convert literals to objects of type std::chrono::duration (i.e. h, min, s, ms, us,ns), std::complex and basic_string.

Example
// $Id: A13-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
constexpr unsigned long long int
operator"" _ft(unsigned long long int feet) // Non-compliant
{
  // Implementation
  return feet;
}
// constexpr std::uint64_t operator"" _m(std::uint64_t meters) // Non-compliant
//{
//    // Implementation
//    return meters;
//}
void Fn() noexcept {
  unsigned long long int feets = 200_ft;
  // std::uint64_t meters = 300_m; // Compilation error - uint64_t has
  // invalid
  // type, not compatible with user-defined literals. On 64-bit machines,
  // uint64_t is defined as unsigned long, and not unsigned long long
}
See Also
  • none

Possible Messages

Key

Text

Severity

Disabled

user_defined_literal

User-defined literals shall not be used.

None

False

Options