Something is odd about the C++ rule for using namespage in a header file

cpp
rules

(Thomas A. F. Thorne) #1

Within our C++ project we are getting an issue flagged up within a .hpp file. Specifically it is ‘“using” directives should not be used in header files’ * cpp:S1003. I would not expect this to be being flagged at present as the code is as follows:

template <class Proto>
void setTimeFrom(const Application::data_shot_t& shot, Proto& msg) {
using namespace std;
using namespace std::chrono;
...
}

Both the using namespace lines are being flagged but both are within a function scope. The rule its self includes in its description

  • MISRA C++:2008, 7-3-6 - using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used in header files.

My only thought at present is the rule as implemented forbids all use of using in a header file but the MISRA statement only requires items outside of a function scope. On that basis, I am starting this off as a bug as the rule does not match the MISRA statement. That might all be beside the point if * ISO/IEC 14882:2003, sections 7.3.3, 7.3.4 then requires they all be excluded.

I am using SonarQube scanner to analyses a C++ and C project. We use the build wrapper around the GNU compile chain within GNU make. Server reports the version as * Version 7.0 (build 36138)

#bug:fp


(Loïc Joly) #2

Hello Thomas,

The Misra rule you quoted says (emphasis is mine):

  • MISRA C++:2008, 7-3-6 - using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used in header files.

In your sample code, what is inside of the function scope is a using directive, so I think we are right to report this code.

What do you think?


(Thomas A. F. Thorne) #3

I get those mixed up. Is this the correct meaning?

Declaration being using a specific type.
Directive being used all types under a namespace.

If it is then I agree you are correct and I shall double check that amending our code results in the issue going away.


(Loïc Joly) #4

Yes.
Using directive is using namespace xxx; while using declaration is using xxx::something;


(Thomas A. F. Thorne) #5

Thank you, that has clarified what SonarQube was correctly trying to tell me. I can confirm that switching to the declarations has resolved the issues as expected.

Perhaps adding a couple more good vs bad examples to the expansion text of the rule would keep someone from mixing up the technical terminology in the future. That is certainly not a bug though.

Once again thank you for your help.