Repeated evaluation of a string-literal may yield different objects
5.13.5 [lex.string]
Balog Pal

Created on 2023-07-15.00:00:00 last changed 7 months ago


Date: 2023-07-15.00:00:00

Prior to the resolution of issue 1823 (incorporated into C++17), the specification required in 9.2.3 [dcl.fct.spec] paragraph 4:

A string literal in the body of an extern inline function is the same object in different translation units.

That was changed to the following specification in 5.13.5 [lex.string] paragraph 9:

Evaluating a string-literal results in a string literal object with static storage duration (6.7.5 [basic.stc]). ... whether successive evaluations of a string-literal yield the same or a different object is unspecified.

The treatment of string literal objects was harmonized with backing arrays in P2752R3 (approved in June, 2023) and issue 2753.

The rationale for the change made by issue 1823 was deviating implementation practice: Implementations do not unify string literal objects in inline functions or template instantiations across different translation units.

However, that was a silent behavior change for examples such as:

  const char *f() { return "abc"; }
  bool b = f() == f();    // guaranteed true prior to CWG1823, now unspecified

The matter should be revisited in light of this new information.

Date User Action Args
2023-07-15 00:00:00admincreate