Your compiler ultimately operates on things called translation units, informally called source files. Within these translation units, you identify different entities: objects, functions, etc. The linkers job is to connect these units together, and part of that process is merging identities.
Identifiers have linkage?: internal linkage means that the entity named in that translation unit is only visible to that translation unit, while external linkage means that the entity is visible to other units.
When an entity is marked static
, it is given internal linkage. So given these two translation units:
// a.cpp
static void foo() { /* in a */ }
// b.cpp
static void foo() { /* in a */ }
Each of those foo
's refer to an entity (a function in this case) that is only visible to their respective translation units; that is, each translation unit has its own foo
.
Here's the catch, then: string literals are the same type as static const char[..]
. That is:
// str.cpp
#include <iostream>
// this code:
void bar()
{
std::cout << "abc" << std::endl;
}
// is conceptually equivalent to:
static const char[4] __literal0 = {'a', 'b', 'c', 0};
void bar()
{
std::cout << __literal0 << std::endl;
}
And as you can see, the literal's value is internal to that translation unit. So if you use "abc"
in multiple translation units, for example, they all end up being different entities.?
Overall, that means this is conceptually meaningless:
template <const char* String>
struct baz {};
typedef baz<"abc"> incoherent;
Because "abc"
is different for each translation unit. Each translation unit would be given a different class because each "abc"
is a different entity, even though they provided the "same" argument.
On the language level, this is imposed by saying that template non-type parameters can be pointers to entities with external linkage; that is, things that do refer to the same entity across translation units.
So this is fine:
// good.hpp
extern const char* my_string;
// good.cpp
const char* my_string = "any string";
// anything.cpp
typedef baz<my_string> coherent; // okay; all instantiations use the same entity
?Not all identifiers have linkage; some have none, such as function parameters.
? An optimizing compiler will store identical literals at the same address, to save space; but that's a quality of implementation detail, not a guarantee.