Home » C++ » Defining global constant in C++

Defining global constant in C++

Posted by: admin November 30, 2017 Leave a comment

Questions:

I want to define a constant in C++ to be visible in several source files.
I can image the following ways to define it in a header file:

  1. #define GLOBAL_CONST_VAR 0xFF
  2. int GLOBAL_CONST_VAR = 0xFF;
  3. Some function returing the value (e.g. int get_GLOBAL_CONST_VAR())
  4. enum { GLOBAL_CONST_VAR = 0xFF; }
  5. const int GLOBAL_CONST_VAR = 0xFF;
  6. extern const int GLOBAL_CONST_VAR;
    and in one source file const int GLOBAL_CONST_VAR = 0xFF;

Option (1) – is definitely not the option you would like to use

Option (2) – defining instance of the variable in each object file using the header file

Option (3) – IMO is over killing in most cases

Option (4) – in many cases maybe not good since enum has no concrete type (C++0X will add possibility to define the type)

So in most cases I need to choose between (5) and (6).
My questions:

  1. What do you prefer (5) or (6)?
  2. Why (5) is ok, while (2) is not?
Answers:

(5) says exactly what you want to say. Plus it lets the compiler optimize it away most of the time. (6) on the other hand won’t let the compiler ever optimize it away because the compiler doesn’t know if you’ll change it eventually or not.

Questions:
Answers:

Definitely go with option 5 – it’s type safe and allows compiler to optimize (don’t take address of that variable 🙂 Also if it’s in a header – stick it into a namespace to avoid polluting the global scope:

// header.hpp
namespace constants
{
    const int GLOBAL_CONST_VAR = 0xFF;
    // ... other related constants

} // namespace constants

// source.cpp - use it
#include <header.hpp>
int value = constants::GLOBAL_CONST_VAR;

Questions:
Answers:

(5) is “better” than (6) because it defines GLOBAL_CONST_VAR as an Integral Constant Expression (ICE) in all translation units. For example, you will be able to use it as array size and as case label in all translation units. In case of (6) GLOBAL_CONST_VAR will be an ICE only in that translation unit where it is defined and only after the point of definition. In other translation units it won’t work as ICE.

However, keep in mind that (5) gives GLOBAL_CONST_VAR internal linkage, meaning that the “address identity” of GLOBAL_CONST_VAR will be different in each translation unit, i.e. the &GLOBAL_CONST_VAR will give you a different pointer value in each translation unit. In most usage cases this doesn’t matter, but if you’ll need a constant object that has consistent global “address identity”, then you’d have to go with (6), sacrificing the ICE-ness of the constant in the process.

Also, when the ICE-ness of the constant is not an issue (not an integral type) and the size of the type grows larger (not a scalar type), then (6) usually becomes a better approach than (5).

(2) is not OK because the GLOBAL_CONST_VAR in (2) has external linkage by default. If you put it in header file, you’ll usually end up with multiple definitions of GLOBAL_CONST_VAR, which is an error. const objects in C++ have internal linkage by default, which is why (5) works (and which is why, as I said above, you get a separate, independent GLOBAL_CONST_VAR in each translation unit).

Questions:
Answers:

If it’s going to be a constant then you should mark it as a constant – that’s why 2 is bad in my opinion.

The compiler can use the const nature of the value to expand some of the maths, and indeed other operations that use the value.

The choice between 5 and 6 – hmm; 5 just feels better to me.

In 6) the value is unnecessarily detached from it’s declaration.

I typically would have one or more of these headers that only defines constants etc within them, and then no other ‘clever’ stuff – nice lightweight headers that can easily be included anywhere.

Questions:
Answers:
const int GLOBAL_CONST_VAR = 0xFF;

because it is a constant!

Questions:
Answers:

To answer your second question:

(2) is illegal because it violates the One Definition Rule. It defines GLOBAL_CONST_VAR in every file where it’s included, i.e. more than once.
(5) is legal because it’s not subject to the One Definition Rule. Each GLOBAL_CONST_VAR is a separate definition, local to that file where it’s included. All those definitions share the same name and value of course, but their addresses could differ.

Questions:
Answers:
#define GLOBAL_CONST_VAR 0xFF // this is C code not C++
int GLOBAL_CONST_VAR = 0xFF; // it is not constant and maybe not compilled
Some function returing the value (e.g. int get_LOBAL_CONST_VAR()) // maybe but exists better desision
enum { LOBAL_CONST_VAR = 0xFF; } // not needed, endeed, for only one constant (enum elms is a simple int, but with secial enumeration)
const int GLOBAL_CONST_VAR = 0xFF; // it is the best
extern const int GLOBAL_CONST_VAR; //some compiller doesn't understand this

Questions:
Answers:

It depends on your requirements. (5) is the best for most normal usage, but often results in the constant taking up storage space in every object file. (6) can get around this in situations where it’s important.

(4) is also a decent choice if your priority is guaranteeing that storage space is never allocated, but it only works for integral constants of course.