Internal and External Linkage

Linkage of Constants

Why do const and constexpr variables have internal linkage by default?

Abstract art representing computer programming

In C++, const and constexpr variables have internal linkage by default to ensure that their values remain consistent across translation units and to avoid potential linking conflicts.

Internal Linkage

  • Internal Linkage: By default, const and constexpr variables are only accessible within the file they are defined in.
  • Reasoning: This prevents different parts of the program from inadvertently modifying these variables and ensures their values are consistent wherever used.

Here’s an example:

// constants.cpp
const int MaxUsers{100};
constexpr float Pi{3.14159f};

Explanation

  • Consistency: Having internal linkage ensures that MaxUsers and Pi are consistent within constants.cpp and can't be altered by other files.
  • Avoiding Conflicts: Internal linkage prevents multiple definition errors that could arise if these variables were defined in multiple files.

Changing Linkage

You can give const and constexpr variables external linkage using the extern keyword:

// constants.h
#pragma once

extern const int MaxUsers;
extern constexpr float Pi;
// constants.cpp
#include "constants.h"

const int MaxUsers{100};
constexpr float Pi{3.14159f};

Below, we access and use these variables:

// main.cpp
#include <iostream>
#include "constants.h"

int main() {
  std::cout << "MaxUsers: " << MaxUsers << '\n';
  std::cout << "Pi: " << Pi << '\n';
}
g++ main.cpp constants.cpp -o myProgram
./myProgram
MaxUsers: 100
Pi: 3.14159

Benefits of Internal Linkage

  1. Encapsulation: Keeps the variable's scope limited to the defining file, promoting encapsulation and modularity.
  2. Avoids Multiple Definitions: Prevents multiple definition errors and ensures a single definition per translation unit.
  3. Consistency: Ensures the value of const and constexpr variables remains consistent across the file, avoiding accidental changes from other parts of the program.

Summary

  • Default Behavior: By default, const and constexpr variables have internal linkage, making them file-local.
  • Purpose: This design choice avoids linking conflicts and ensures value consistency.
  • Modifying Linkage: If needed, their linkage can be changed to external using the extern keyword.

Understanding the default internal linkage of const and constexpr variables helps in writing clear and conflict-free code, ensuring variables are used as intended without unintended side effects.

This Question is from the Lesson:

Internal and External Linkage

A deeper look at the C++ linker and how it interacts with our variables and functions. We also cover how we can change those interactions, using the extern and inline keywords

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Internal and External Linkage

A deeper look at the C++ linker and how it interacts with our variables and functions. We also cover how we can change those interactions, using the extern and inline keywords

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved