Main Content

AUTOSAR C++14 Rule A3-1-5

A function definition shall only be placed in a class definition if (1) the function is intended to be inlined (2) it is a member function template (3) it is a member function of a class template

Since R2020b

Description

Rule Definition

A function definition shall only be placed in a class definition if (1) the function is intended to be inlined (2) it is a member function template (3) it is a member function of a class template.

Rationale

Placing a function definition in a class definition is allowed only if:

  • The function is intended to be inlined. Placing the definition of a member function in the class definition instructs the compiler to inline the member function. Inlining small functions avoids the run-time overhead of function calls and improves the performance of the compiled executable. But if you place the definition of a large member function inside the class definition unaware of this implicit inlining, the compiled executable might be too large.

  • The function is a member function template or a member of a class template. These coding practices reduce repetitions of template syntax elements (for example, the parameter list). This reduction improves the readability and maintainability of the code.

Polyspace Implementation

The checker uses the heuristic that, unless you explicitly use the inline keyword, you intend to inline only small functions that consist of no more than one statement. The checker interprets AUTOSAR C++14 Rule A3-1-5 in the following way.

For nontemplate member functions and member functions of nontemplate classes, the checker flags one-line member functions defined outside a class and larger member functions defined inside a class.

For template member functions and member functions of template classes, the checker flags any member function that is defined outside a class.

Polyspace® does not report a violation of this rule for constexpr and consteval functions.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>
#include <iostream>

class A
{
	private:
	std::uint32_t val = 5;
	
	public:
	std::uint32_t Foo()  // Compliant with (1)
	{
		return val;
	}
	
	std::uint32_t Bar(); 
};

std::uint32_t A::Bar() // Noncompliant with (1)
{
	return (val + 5);
} 

std::uint32_t main()
{
	A a;
	std::cout << a.Foo() << std::endl;
	std::cout << a.Bar() << std::endl;
	return 0;
}

The placement of the definition of Bar outside the definition of class A violates the rule, because Bar consists of a single statement.

#include <cstdint>
#include <iostream>

class A
{
  public:
    template <typename T>     // Compliant with (2)
    void Foo(T t)
    {
      std::cout << "This function is defined inside with param: " 
      << t << std::endl;
    }

    template <typename T>     // Non-compliant with (2)
    void Bar(T t);
};


template <typename T>
void A::Bar(T t)
{
  std::cout << "This function is defined outside with param: " 
  << t << std::endl;
}

std::uint32_t main(void)
{
  A a;
  a.Foo<float>(3.14f);
  a.Bar<std::uint32_t>(5);
  return 0;
}

The placement of the definition of the member function template Bar outside the definition of class A violates the rule.

#include <cstdint>
#include <iostream>

template <typename T>
class B
{
  public:
    B(const T x) : t(x) {}

    void display()   //Compliant with (3)
    {
      std::cout << t << std::endl;
    }

    void display2();   //Non-compliant with (3)

  private:
    T t;
};

template <typename T>
void B<T>::display2()
{
  std::cout << t << std::endl;
}

int main(void)
{
  B<std::int32_t> b(7);
  b.display();
  b.display2();
  return 0;
}

The placement of the definition of the member function display2 outside the definition of the class template B violates the rule.

Check Information

Group: Basic concepts
Category: Required, Partially automated

Version History

Introduced in R2020b

expand all