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.

Recommended Answers

All 2 Replies

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.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.