Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.FUNC.DECL.AFTERUSE

Function chosen by overload resolution when instantiating a template is declared after its usage.

MISRA-C++ Rule 14-6-2 (required): The function chosen by overload resolution shall resolve to a function declared previously in the translation unit.

Rationale

Argument-dependent lookup (ADL) adds additional associated namespaces to the set of scopes searched when lookup is performed for the names of called functions. For function templates, ADL is performed at the point of instantiation of the function template, and so it is possible that a function declared after the template may be called.

To ensure that ADL does not take place when calling a function with a dependent argument, the postfix-expression denoting the called function can either be a qualified name or a parenthesized expression.

Example

void b ( int32_t );

template <typename T>
void f ( T const & t )
{
   b ( t );        // Non-compliant - Calls NS::b declared after f
   ::b ( t );      // Compliant - Calls ::b
   ( b ) ( t );    // Compliant - Calls ::b
}
namespace NS
{
   struct A
   {
      operator int32_t ( ) const;
   };
   void b ( A const & a );
}
int main ( )
{
   NS::A a;
   f ( a );
}

Operators with dependent types may also have this problem. In order to avoid ADL in these examples, operators should not be overloaded, or the calls should be changed to use explicit function call syntax and a qualified name or parenthesized expression used, as above.

For example:

template <typename T>
void f ( T const & t )
{
   t == t;                           // Non-compliant - Calls NS::operator==
                                     // declared after f
   ::operator ==( t, t );            // Compliant - Calls built-in operator==
   ( operator == <T> ) ( t, t );     // Compliant - Calls built-in operator==
}
namespace NS
{
   struct A
   {
      operator int32_t ( ) const;
   };
   bool operator== ( A const &, A const & );
}
int main ( )
{
   NS::A a;
   f ( a );
}