2

I have a number of templatic and inline functions defined in an header file (let's call it head.h). Currently, these functions use some global variables declared with extern in the same header and defined somewhere in one .cpp.

Everything works fine, but I would limit global variables scope only to the header where they are defined.

In other words every other .h or .cpp that includes my head.h wouldn't be able to see such globals.

Any way to achieve this result?

7
  • Why do you want to do that? If multiple includers need to use the functions which in turn need to access the same global variable, how do you want to prevent the includers from seeing and using the global variable? Commented Feb 17, 2016 at 16:00
  • @WernerHenze because i don't want variable pollution. these variables must be used only in that header. if functions weren't inline i would define my globals in cpp as static, limiting their scope to that cpp Commented Feb 17, 2016 at 16:08
  • or anonymous namespace as someone suggested below Commented Feb 17, 2016 at 16:10
  • I see but if several functions in the header use these variables at the same time, I would probably create a class around it. If possible, the variables could also be passed from one function to another. I think, a really nice solution depends a lot on your actual application Commented Feb 17, 2016 at 16:17
  • @IceFire I agree and I know benefits of well written and scalable code. My question here is mainly for interest. To be honest I don't know why such possibility isn't available in C. Commented Feb 17, 2016 at 16:22

3 Answers 3

3

including header files just copies the content of the header file into the including cpp/h file. That means, you cannot really distinguish whether you do something in your header file or in a cpp/h file that includes your header file.

So, variables that are defined in a header file cannot be excluded. If they are in a cpp file and you would like to forbid extern, you could use an anonymous namespace:

namespace
{
    int variable;
}

Best practice, obviously, would be to not use global variables at all. It is considered bad style for several reasons like readability, hard to determine dependencies, difficulties with testing, a really bad chance of extending your software and so on. So, you might reconsider this kind of architecture for your next project if refactoring is not an option here.

Sign up to request clarification or add additional context in comments.

2 Comments

First this creates one variable for each including cpp, not one global variable for them all. Second it does not prevent polluting global namespace, which seems to be what the questioner wants.
Please read my post carefully. "If they are in a cpp file ..." is clearly said in the sentence before the code fragment. Nothing is included in that scenario.
1

One way would be to put a class around it instead of a namespace, making all functions public static methods of that class and the global variables private static variables.

i.e.:

head.h:

class A
{
  public:
    template <typename T> static void MethodA(T const &value)
    {
      //...
    }
    inline static void MethodB(int a, int b)
    {
      // ...
    }

  private:
    static int x;
    static std::string y;
};

head.cpp:

int A::x = 0;
std::string A::y;

EDIT:

Another alternate method would be to define the globals as private static variables in a class and make all functions that are allowed to use them friend functions:

head.h:

template <typename T> void FunctionA(T const &value);
void FunctionB(int a, int b);

class Vars
{
  friend template <typename T> void FunctionA(T const &value);
  friend FunctionB(int a, int b);

  private:
    static int x;
    static std::string y;
};

template <typename T> void FunctionA(T const &value)
{
  // ...
  // ... A::x ...
  // ...
}
inline void FunctionB(int a, int b)
{
  // ...
  // ... A::y ...
  // ...
}

head.cpp:

int A::x = 0;
std::string A::y;

1 Comment

yep, even if the simpler and possibly most correct answer was "no", I were asking for a solution and I think you hit the spot
0

No, it is not possible. If you declare some variable as extern in a header1.h - this variable will be available in any other headers which include header1.h.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.