On 2017-08-14 03:48, Brian Bi wrote:
> According to the current draft, "whether successive evaluations of
> a/string-literal
> <
http://eel.is/c++draft/lex.string#nt:string-literal>/yield the same or
> a different object is unspecified".
>
> This wording seems to suggest that in the following program, two
> different addresses may be printed:
>
> #include <iostream>
> void foo() {
> const char* bar = "bar";
> std::cout << (void*)bar << '\n';
> }
> int main() {
> foo();
> foo();
> }
>
> This is based on the interpretation that the string-literal is evaluated
> once every time the function foo() is called.
>
> However, the same paragraph also says that "Evaluating a/string-literal
> <
http://eel.is/c++draft/lex.string#nt:string-literal>/results in a
> string literal object with static storage duration". This suggests that
> the evaluation of a local string literal happens once, as if it were any
> other static local variable. After all, surely it was not intended that
> a program can contain an unbounded number of copies of the string
> literal object that all have static storage duration.
>
> I wonder whether a wording change is needed to clarify the situation?
I believe the intention is to clarify this case
int main()
{
const char* bar1 = "bar";
const char* bar2 = "bar";
std::cout << (void*)bar1 << (void*)bar2 << '\n';
}
and that bar1 and bar2 might, or might not, contain the same address.
Bo Persson