Answer the question
In order to leave comments, you need to log in
What is the binding of a namespace defined in the scope of a .cpp file?
What is the binding of a namespace defined in the scope of a .cpp file?
For example, in this code:
#include "header.hpp"
namespace Chrono
{
Date &Date::add_year(int n)
{
if (d == 29 && m == 2 )
{
d = 1;
m = 3;
}
y += n;
return *this;
}
void Date::set_default()
{
d = 1;
m = 1;
y = 1970;
}
}
Answer the question
In order to leave comments, you need to log in
Files with the extension .cpp
are usually assembly points for translation modules [ ? ].
The binding characteristics only have their effect between translation units. The linker deals with linking the code and works with the results of processing exactly the translation modules - object files.
Therefore, if you .cpp
connect the other in one .cpp
(excluded from the assembly in other ways), then all elements with internal binding of any of these .cpp
will be available in both of them.
This will also be true for files with the extension .h
. Files .cpp
usually include .h
and together form a translation unit with their code, in which all elements with internal linking are available. Even in the code files .h
.
This is important to consider so as not to make some mistakes.
On the merits of the question. All non-static (without the specifier static
) members of named namespaces have an outer binding [ ? ]. The global space is also named (and accessible through the ::
unnamed on the left) and in exactly the same way endows all of its non-static members with the external linkage characteristic.
In contrast, absolutely all members of anonymous namespaces (even members of nested namespaces) have the inner-binding characteristic [ ? ].
A fairly common mistake is to define anonymous spaces and static space elements in headers, after which many translation modules are immediately replenished with internally linked code. This leads to the swelling of the binary code and the complexity of the assembly.
Separately, I would like to designate inline
.
The inline
[ ? ] specifier ] flags weak outer binding for any entity. That a constant or a global variable, that a function or a method (even a static one) are marked as entities with external binding, which does not violate the ODR if all definitions of the binding target in all translation modules are exactly the same. If at least one definition of the binding target differs, there will be an ODR violation.
The linker will pick up the very first definition and embed it into the binary. After that, the linker will only check other encountered definitions for compliance with the first one, and will link the code only relative to the first encountered definition.
Therefore, if in any named space, in several , functions with the same name and the same signature, but different bodies, are .cpp
defined , then problems with the assembly will not be avoided.
All this can be studied in more detail in the article on Habré. inline
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question