Search code examples
c++misra

MISRA C++ Rule 14-5-1: Name of generic function template declared in namespace associated with type


Is Warning 1573 ("Name of generic function template declared in namespace associated with type") really relevant when the namespace is an anonymous namespace? Most of the helper functions I have for tests go in unnamed namespace and it breaks the above rule.

Example:

namespace
{
  template <typename T>
  T template_func(T arg)
  {
    return arg;
  }

  class foo {};
}

int main()
{
  return template_func(0);
}

How do I get around in the above, to satisfy the rule?


Solution

  • As state in their example, you might use extra namespace, something like:

    namespace
    {
        template< class T >
        T template_func(T arg) { return arg; }
    
        namespace X
        {
            class foo{};
        }
        using X::foo;
    }
    
    int main()
    {
        return template_func(0);
    }