Rule 3-9-1

Moderators: david ward, misra cpp

Post Reply
pmhill
Posts: 4
Joined: Wed Apr 08, 2009 9:17 am
Company: University of Leeds

Rule 3-9-1

Post by pmhill » Fri Dec 30, 2011 8:19 pm

The rule states that the types for a function declaration and definition must be token-for-token identical. How literally should this rule be taken. I have two cases where this rule seems to conflict with normal or good practice.

1. In a function definition, a 'const' qualifier is often included for parameters passed by value as a promise that the implementation will not modify these parameters. This qualifier cannot be included in the declaration. For example:

template <typename T>
T low_bits_mask(unsigned n);

template <typename T>
T low_bits_mask(const unsigned n) {
return ~((~static_cast<T>(0)) << n);
}

2. Use of namespace qualifiers.
In the 1st example, the return type has a namespace qualifier in the definition which it is not needed in the declaration.

namespace P {
Linear_Expression
operator+(const_reference n, const Linear_Expression& e);
}

P::Linear_Expression
P::operator+(const_reference n, const Linear_Expression& e)

In the second example the function declaration needs the std namespace qualifier. However, as there is a using directive preceding the function definition, the same parameter does not need the std qualifier.

void print_clock(std::ostream& s);

using namespace std;
void print_clock(ostream& s) { ... }

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 3-9-1

Post by misra cpp » Mon Oct 05, 2015 2:43 pm

In the examples quoted, there are not two instances of the same function.

Rule 3-9-1 relates to the case where the same function is declared in two translation units and these two declarations must obey the 'One Definition Rule'.

Token-for-token compliance is the easiest way to ensure this.
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.3 Basic concepts (C++)”