AutosarC++17_10-A13.3.1

A function that contains “forwarding reference” as its argument shall not be overloaded

Required inputs: IR

Rationale

A template parameter that is declared “T&&” (Scott Meters called it a “universal reference”, while C++ Language Standard calls it a “forwarding reference”) will deduce for any type. Overloading functions with “forwarding reference” argument may lead to developer’s confusion on which function will be called.

Exceptions

Declaring an overloading function that takes a “forwarding reference” parameter to be “=delete” does not violate this rule.

Example
// $Id: A13-3-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
template <typename T> void F1(T &&t) noexcept(false) {}
void F1(std::int32_t &&t) noexcept // Non-compliant - overloading a function
                                   // with forwarding reference
{}
template <typename T> void F2(T &&t) noexcept(false) {}
void F2(std::int32_t &) = delete; // Compliant by exception

int main(int, char **) {
  std::int32_t x = 0;
  F1(x);  // Calls f1(T&&) with T = int&
  F1(+x); // Calls f1(std::int32_t&&)
  F1(0);  // Calls f1(std::int32_t&&)
  F1(0U); // Calls f1(T&&) with T = unsigned int
  F2(0);  // Calls f2(T&&) with T = int
          // f2(x); // Compilation error, the overloading function is deleted
}
See Also
  • HIC++ v4.0 [8]: 13.1.2 If a member of a set of callable functions includes a universal reference parameter, ensure that one appears in the same position for all other members.
  • Effective Modern C++ [12]: Item 26. Avoid overloading on universal references.

Possible Messages

Key

Text

Severity

Disabled

overloaded_fref

Functions that contain forwarding reference parameters shall not be overloaded

None

False

Options

only_report_overloading_on_forwarding_references

only_report_overloading_on_forwarding_references : bool = False

Whether to enable a slightly different rule interpretation: with a forwarding reference parameter are reported if there exists another function in the overloaded set that overloads the function with the forwarding reference parameter on that forwarding reference parameter.