The following quote is from C++ Templates by Addison Wesley. Could someone please help me understand in plain English/layman’s terms its gist?
Because string literals are objects with internal linkage (two string literals with the same value but in different modules are different objects), you can’t use them as template arguments either:
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: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 ownfoo.Here’s the catch, then: string literals are the same type as
static const char[..]. That is: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:
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:
†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.