Literally: string literals – part 1

String literals is a topic that often comes up when writing critical sections of code. Usually most developers don’t care too much about literals, since in general, they just work. But then you might do some profiling and realize you’re doing excessive amounts of calls to string related functions.

Ordinary string literals (aka narrow string literals) are an array of n const char. A string literal also has static storage duration. A reason why they need to be “const”, is that the standard hints that they may be optimized. Meaning they shouldn’t be modified:


Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified. [ Note: The effect of attempting to modify a string literal is undefined. —end note “

standard [lex.string])
Continue readingLiterally: string literals – part 1

Non-type template arguments

The usual method to employ templates is to supply a type to its parameters. Most C++ beginners at some point have used container, and supplied the value type of the container (and possibly more types for the other parameters such as an allocator type, which are in fact template template arguments).

Less common (in my experience) are nontype template arguments. A simple definition would look like this:

template <typename T, T nontypeParam>
class A
{
...
};

A<int,10> a;

There is also the option of specifying the type directly in the definition:
Continue readingNon-type template arguments