Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String literals not allowed as non type template parameters

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:

like image 467
Aquarius_Girl Avatar asked Apr 05 '11 06:04

Aquarius_Girl


People also ask

Which parameter is allowed for non-type template?

Which parameter is legal for non-type template? Explanation: The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

Which is correct example of template parameters?

For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.

What is a non-type in C++?

A non-type template argument provided within a template argument list is an expression whose value can be determined at compile time. Such arguments must be constant expressions, addresses of functions or objects with external linkage, or addresses of static class members.

How do you use template arguments in C++?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)


2 Answers

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.

like image 120
GManNickG Avatar answered Oct 08 '22 02:10

GManNickG


It means you can't do this...

#include <iostream>  template <const char* P> void f() { std::cout << P << '\n'; }  int main() {     f<"hello there">(); } 

...because "hello there" isn't 100% guaranteed to resolve to a single integral value that can be used to instantiate the template once (though most good linkers will attempt to fold all usages across linked objects and produce a new object with a single copy of the string).

You can, however, use extern character arrays/pointers:

... extern const char p[]; const char p[] = "hello"; ...     f<p>(); ... 
like image 43
Tony Delroy Avatar answered Oct 08 '22 01:10

Tony Delroy