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) { ... }
Rule 3-9-1
Moderators: david ward, misra cpp
-
- Posts: 154
- Joined: Mon Jun 02, 2008 1:55 pm
- Company: MISRA
Re: Rule 3-9-1
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.
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
the MISRA C++ Working Group