Rule 8–4–4 (Required) function identifier with & or ()

Moderators: david ward, misra cpp

Post Reply
Achim Olaf Zacher
Posts: 4
Joined: Wed Sep 14, 2005 9:59 am
Location: Germany

Rule 8–4–4 (Required) function identifier with & or ()

Post by Achim Olaf Zacher » Wed Aug 13, 2014 8:38 pm

Is it intended to ban the use of

Code: Select all

std::cout << "a:" << a << std::endl;
and to enforce the use of

Code: Select all

std::cout << "a:" << a;
std::endl (std::cout); 
instead?

roberto
Posts: 11
Joined: Wed Jun 04, 2008 10:33 pm
Company: University of Parma, Italy

Re: Rule 8–4–4 (Required) function identifier with & or ()

Post by roberto » Thu Jul 07, 2016 3:29 pm

The rationale of the rule clearly explains that what has to be avoided is the possible mistake where

Code: Select all

  if ( f ) { ... }
is written where

Code: Select all

  if ( f() ) { ... }
was intended. The former fragment compiles because of an implicit conversion to pointer (decay) and boolean evaluation.

In order to achieve this effect, the rule is so strict that (if interpreted literally) would ban the idiomatic

Code: Select all

  std:cout << std::endl;
where an innocuous implicit conversion to pointer takes place, and force people to write (the horrible)

Code: Select all

  std:cout << &std::endl;
I think the simplest way out is to rephrase the "Exception" as follows, where the part in asterisks has been added:
Passing the function by reference *or by pointer where the formal parameter is a pointer to the very same function type*, or assigning it to a reference object is not a violation of this rule.

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

Re: Rule 8–4–4 (Required) function identifier with & or ()

Post by misra cpp » Mon Oct 31, 2016 3:17 pm

As stated, std::cout << "a:" << a << std::endl; is a violation, but as you point out is well defined and common usage

At present, it can be allowed by deviation

This will be made an explicit exception in a Technical Corrigendum
Posted by and on behalf of
the MISRA C++ Working Group

grunwald
Posts: 15
Joined: Wed May 14, 2014 1:02 pm
Company: Axivion GmbH

Re: Rule 8–4–4 (Required) function identifier with & or ()

Post by grunwald » Thu Dec 01, 2016 4:37 pm

Here's another case that seems like a violation by the current formulation of 8-4-4 (though it clearly shouldn't be):

class DerivedClass : private BaseClass
{
public:
using BaseClass::getValue; // 8.4.4 violation?
};

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

Re: Rule 8–4–4 (Required) function identifier with & or ()

Post by misra cpp » Tue Jan 24, 2017 10:28 am

Currently we are thinking as the following as exception to this rule in the TC
- Passing the function by reference or assigning it to a reference object,
- Use of the name in a 'using declaration'
- Use of iostream items for formatted output, like std::endl
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.8 Declarators (C++)”