Solving Problems With the One Definition Rule
Let's learn about name collisions leading to ambiguity and definition inconsistency.
We'll cover the following
Phil Karlton was right on point when he said the following:
"There are two hard things in computer science: cache invalidation and naming things."
Names are difficult for a few reasons: they have to be precise, simple, short, and expressive at the same time. That makes them meaningful and allows programmers to understand the concepts behind the raw implementation. C++ and many other languages impose one more requirement: many names must be unique.
This is manifested in a few different ways. A programmer is required to follow the .cpp
file), we must define it exactly once, even if we declare the same name (of a variable, function, class type, enumeration, concept, or template) multiple times.
This rule is extended to the scope of an entire program for all variables effectively used in our code and non-inlined functions.
ODR-fail example
Consider the following example. We declare a variable i
in a shared.h
file:
Get hands-on with 1400+ tech skills courses.