“cpp:S3470 Standard namespaces should not be modified” is raised on a forward declaration of a std class.
Steps to reproduce
The following code at the top of a header file will raise the error:
It should not.
Don’t use forward declaration of std classes? This is definitely not a solution though.
Unfortunately, what you would like to achieve is not allowed by the standard, and for good reasons (see for instance GotW #34: Forward Declarations for more details). So we report this issue on purpose.
In practice, it may work today with your compiler, but there is no guarantee of that. For instance, in your standard library,
thread might be changed to become a typedef to another type.
So the best option today is probably to just include
<thread>, and a better option for the hopefully not-too-far-away future (which might already be your present depending on the compilers you are using) will be to use the standard library through modules, which will make optimizing what part of the standard library you include a thing of the past.
All right, I was not aware of this limitation of the std. Thanks for the clarification.
Could you maybe update the hint provided in SonarCloud when this kind of error occur on a forward declaration?
It does indeed talk about “declarations” and “definitions” being undefined behavior, but at first glance it was not obvious to me that “forward declarations” were also impacted by this restriction.
The current hint explains clearly why you should not attempt to declare or define new things or attempting to redefine existing methods inside the std namespace, but the fact that forward declaring classes might not work is also really important and should be clearly mentionned, and maybe added to the example too.
Thank you for your suggestion. I created a ticket to improve our documentation.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.