Internal and External Linkage

Using Inline Variables

What are the advantages of using inline variables over other methods to avoid multiple definitions?

Abstract art representing computer programming

Using inline variables, introduced in C++17, offers several advantages over traditional methods to avoid multiple definitions.

These variables are particularly useful when you need a global variable to be accessible across multiple translation units without causing linker errors.

Advantages of inline Variables

Avoiding Multiple Definitions:

  • inline variables can be defined in header files and included in multiple source files without violating the One Definition Rule (ODR).
  • The compiler treats multiple instances of inline variables as a single definition.

Simpler Syntax:

  • The inline keyword provides a straightforward way to define variables without the need for separate declarations and definitions.
  • This reduces boilerplate code and makes the codebase easier to maintain.

Here’s an example:

// globals.h
#pragma once

inline int GlobalVar{42};
// main.cpp
#include <iostream>
#include "globals.h"

int main() {
  std::cout << "GlobalVar: " << GlobalVar;
}
// other.cpp
#include "globals.h"

// Additional code can use GlobalVar here
g++ main.cpp other.cpp -o myProgram
./myProgram
GlobalVar: 42

Comparison with Other Methods

Extern Keyword: Without inline, you would need to declare the variable with extern in the header file and define it in a source file. This method involves more boilerplate and separates the declaration and definition.

// globals.h
#pragma once

extern int GlobalVar;
// globals.cpp
int GlobalVar{42};
// main.cpp
#include <iostream>
#include "globals.h"

int main() {
  std::cout << "GlobalVar: " << GlobalVar;
}

Static Keyword: Using static for internal linkage prevents multiple definitions but limits the variable's scope to the defining file.

// globals.cpp
static int GlobalVar{42};

This means GlobalVar cannot be accessed from other files.

Benefits of inline

  • Consistency: Ensures that the variable is treated as a single entity across all translation units.
  • Encapsulation: Provides the convenience of defining variables in headers while maintaining modular code structure.
  • Maintenance: Simplifies the codebase by reducing the need for separate declarations and definitions.

Summary

  • Inline Variables: Introduced in C++17, they allow global variables to be defined in header files without causing multiple definition errors.
  • Advantages: Simplifies syntax, avoids boilerplate, and ensures consistency across translation units.
  • Comparison: More convenient and maintainable compared to traditional methods like extern and static.

Using inline variables makes global variable management easier and more efficient, especially in large projects with multiple source files.

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