0

Given this header

// header.h
#include <string>

namespace company
{
  namespace module
  {
    class ProjectConstants
    {
    public:
      static const int CONSTANT1;
      static const std::string CONSTANT2;
    };
  }
}

and this source file

// header.cpp
#include "header.h"

using company::module::ProjectConstants;


const int ProjectConstants::CONSTANT1 = 10;
const std::string ProjectConstants::CONSTANT2("Hello World");

Are ProjectConstants::CONSTANT1 and ProjectConstants::CONSTANT2 declared in the global namespace or not?

I am asking this because our project follows a coding standard that requires that the global namespace is not polluted and I thought that this wouldn't do that, however when using static analysis, FlexeLint, it says that the code at lines 7 and 8 of the cpp file place the symbols in the global namespace.

3
Contributors
2
Replies
17
Views
3 Years
Discussion Span
Last Post by mike_2000_17
0

Are ProjectConstants::CONSTANT1 and ProjectConstants::CONSTANT2 declared in the global namespace or not?

A using declaration introduces names to the current namespace, so in this case yes, they are indeed in the global namespace for that particular translation unit.

0

It makes no difference whatsoever when it comes to global symbols.

I tried the following source file:

#include "header.h"

#if 0

using company::module::ProjectConstants;
const int ProjectConstants::CONSTANT1 = 10;
const std::string ProjectConstants::CONSTANT2("Hello World");

#else

namespace company
{
  namespace module
  {
    const int ProjectConstants::CONSTANT1 = 10;
    const std::string ProjectConstants::CONSTANT2("Hello World");
  };
};

#endif

And I get the exact same list of symbols on the object file in both cases (#if 0 and #if 1).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.