Title
Same address for string literals from default arguments in inline functions?
Status
cd1
Section
9.2.3 [dcl.fct.spec]
Submitter
Mark Mitchell

Created on 2003-01-13.00:00:00 last changed 188 months ago

Messages

Date: 2007-04-15.00:00:00

[Voted into WP at April, 2007 meeting.]

Date: 2006-04-15.00:00:00

Proposed resolution (April, 2006):

Change the last sentence and add the note to the end of 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. [Note: A string literal appearing in a default argument expression is not considered to be “in the body” of an inline function merely by virtue of the expression’s use in a function call from that inline function. —end note]
Date: 2003-10-15.00:00:00

Notes from October 2003 meeting:

We discussed ctor-initializer lists and decided that they are also part of the body. We've asked Clark Nelson to work on syntax changes to give us a syntax term for the body of a function so we can refer to it here. See also issue 452, which could use this term.

(October, 2005: moved to “review” status in concert with issue 452. With that resolution, the wording above needs no further changes.)

Date: 2003-04-15.00:00:00

Proposed resolution (April 2003):

Change the last sentence and add the note to the end of 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. [Note: A string literal that is encountered only in the context of a function call (in the default argument expression of the called function), is not “in” the extern inline function.]
Date: 2003-04-15.00:00:00

Notes from April 2003 meeting:

The core working group feels that the address of a string literal should be guaranteed to be the same only if it actually appears textually within the body of the inline function. So a string in a default argument expression in a block extern declaration inside the body of a function would be the same in all instances of the function. On the other hand, a string in a default argument expression in the header of the function (i.e., outside of the body) would not be the same.

Date: 2003-01-13.00:00:00

Are string literals from default arguments used in extern inlines supposed to have the same addresses across all translation units?

  void f(const char* = "s")
  inline g() {
    f();
  }

Must the "s" strings be the same in all copies of the inline function?

Steve Adamczyk: The totality of the standard's wisdom on this topic is (9.2.3 [dcl.fct.spec] paragraph 4):

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

I'd hazard a guess that a literal in a default argument expression is not "in" the extern inline function (it doesn't appear in the tokens of the function), and therefore it need not be the same in different translation units.

I don't know that users would expect such strings to have the same address, and an equally valid (and incompatible) expectation would be that the same string literal would be used for every expansion of a given default argument in a single translation unit.

History
Date User Action Args
2008-10-05 00:00:00adminsetstatus: wp -> cd1
2007-08-05 00:00:00adminsetstatus: dr -> wp
2007-05-06 00:00:00adminsetmessages: + msg1501
2007-05-06 00:00:00adminsetstatus: ready -> dr
2006-11-05 00:00:00adminsetstatus: review -> ready
2006-04-22 00:00:00adminsetmessages: + msg1322
2005-10-22 00:00:00adminsetstatus: drafting -> review
2003-11-15 00:00:00adminsetmessages: + msg932
2003-11-15 00:00:00adminsetstatus: review -> drafting
2003-04-25 00:00:00adminsetmessages: + msg822
2003-04-25 00:00:00adminsetmessages: + msg821
2003-04-25 00:00:00adminsetstatus: open -> review
2003-01-13 00:00:00admincreate