Topic: 7.1.2/4: static local variables of inline functions


Author: Greg Herlihy <greghe@pacbell.net>
Date: Fri, 18 May 2007 11:08:11 CST
Raw View
On 5/16/07 12:13 PM, in article
1179338900.300634.262940@u30g2000hsc.googlegroups.com, "Vladimir Marko"
<swelef@post.sk> wrote:

> Even in the most recent draft (n2284) the paragraph 7.1.2/4 says
>     ... An inline function with external linkage shall have the same
>     address in all translation units. A static local variable in an
> extern
>     inline function always refers to the same object. A string literal
> in
>     an extern inline function is the same object in different
> translation
>     units.
> The wording makes a distinction between an "inline function with
> external linkage" and an "extern inline function". Is this
> intentional?

What distinction? There is nothing in these three sentences to suggest that
an "extern inline function" and an "inline function with external linkage"
are anything but synonymous. A distinction would require that both terms
appear in a single sentence with some kind of qualifier separating them,
such as "but...", "except...", "provided that...". But as currently worded,
these three sentences are completely independent of each other.

> What about static local variables and string literals of inline
> functions
> with external linkage that are not explicitly declared extern -- do
> they
> cause undefined behaviour?

In C++, if a function is not extern then it must be static. And local
variables and string literals are well-defined in both types of functions,
so the answer is "no."

Greg

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Vladimir Marko <swelef@post.sk>
Date: Fri, 18 May 2007 11:01:56 CST
Raw View
On 17 May, 06:34, jg <jgu...@gmail.com> wrote:
> On May 16, 12:13 pm, Vladimir Marko <swe...@post.sk> wrote:
>
> > Even in the most recent draft (n2284) the paragraph 7.1.2/4 says
> >     ... An inline function with external linkage shall have the same
> >     address in all translation units. A static local variable in an
> > extern
> >     inline function always refers to the same object. A string literal
> > in
> >     an extern inline function is the same object in different
> > translation
> >     units.
> > The wording makes a distinction between an "inline function with
> > external linkage" and an "extern inline function". ...
>
> Isn't an inline function with external linkage the same as extern
> inline function ?!

The problem is that the quoted paragraph uses the _keywords_
"inline" and "extern" (printed in a different font) and thus implies
that the part about static local variables and string literals
applies only to inline functions explicitly declared extern.
If this is not the intent, the paragraph should be reworded.

See recent thread "singleton initialization" in clc++m,
    struct singleton{
        static singleton& instance(){ // implicitly inline
            static singleton s; // undefined behaviour?
            return s;
        }
    };

Regards,
Vladimir Marko

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Fri, 18 May 2007 11:11:40 CST
Raw View
On May 16, 9:13 pm, Vladimir Marko <swe...@post.sk> wrote:
> Even in the most recent draft (n2284) the paragraph 7.1.2/4 says
>     ... An inline function with external linkage shall have the same
>     address in all translation units. A static local variable in an
> extern
>     inline function always refers to the same object. A string literal
> in
>     an extern inline function is the same object in different
> translation
>     units.
> The wording makes a distinction between an "inline function with
> external linkage" and an "extern inline function". Is this
> intentional?
> What about static local variables and string literals of inline
> functions
> with external linkage that are not explicitly declared extern -- do
> they
> cause undefined behaviour?

I think this is just editorial.  If I'm not mistaken, this text
hasn't changed since the original standard, perhaps not even
since the ARM.  In the ARM, an inline function had internal
linkage unless it was explicitly declared extern.  (Note that
the text you quote is in a certain sense a comment.  There's
nothing in the rest of the text which authorizes the compiler to
treat an inline function differently from any other function in
these regards.)

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Sat, 19 May 2007 21:58:43 CST
Raw View
On May 18, 7:08 pm, Greg Herlihy <gre...@pacbell.net> wrote:
> On 5/16/07 12:13 PM, in article
> 1179338900.300634.262...@u30g2000hsc.googlegroups.com, "Vladimir Marko"

> <swe...@post.sk> wrote:
> > Even in the most recent draft (n2284) the paragraph 7.1.2/4 says
> >     ... An inline function with external linkage shall have the same
> >     address in all translation units. A static local variable in an
> > extern
> >     inline function always refers to the same object. A string literal
> > in
> >     an extern inline function is the same object in different
> > translation
> >     units.
> > The wording makes a distinction between an "inline function with
> > external linkage" and an "extern inline function". Is this
> > intentional?

> What distinction? There is nothing in these three sentences to suggest that
> an "extern inline function" and an "inline function with external linkage"
> are anything but synonymous.

There's nothing to suggest that they are the same.  In the
actual text in the standard, the words "extern" and "inline" are
in typewriter font, which generally means that the keywords are
meant.  (For that matter, I don't think that there is a word
"extern" in the English language.)  Thus, the text very strongly
suggests that it is the presence of the keywords in the
declaration which is important, and not the actual linkage
(which is "external", and not "extern").

Historically, inline functions had internal linkage, unless
explicitly declared extern, so this distinction didn't make a
difference; an inline function had external linkage if and only
if it was declared "extern".  This was changed sometime in the
1990's, however, and today, inline functions have external
linkage by default.

> A distinction would require that both terms
> appear in a single sentence with some kind of qualifier separating them,
> such as "but...", "except...", "provided that...". But as currently worded,
> these three sentences are completely independent of each other.

> > What about static local variables and string literals of inline
> > functions
> > with external linkage that are not explicitly declared extern -- do
> > they
> > cause undefined behaviour?

> In C++, if a function is not extern then it must be static.

Not at all.  Where do you get that from?  I rarely declare
a function either.

Don't confuse keywords that appear in the declaration (such as
extern) and linkage: extern and static are keywords, where as
the linkage may be external, internal or none.  A function
declared at namespace scope has external linkage, even in the
absence of a keyword, and a function declared at class scope has
external linkage, even if it is static.

--
James Kanze (Gabi Software)            email: james.kanze@gmail.com
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Vladimir Marko <swelef@post.sk>
Date: Wed, 16 May 2007 13:13:38 CST
Raw View
Even in the most recent draft (n2284) the paragraph 7.1.2/4 says
    ... An inline function with external linkage shall have the same
    address in all translation units. A static local variable in an
extern
    inline function always refers to the same object. A string literal
in
    an extern inline function is the same object in different
translation
    units.
The wording makes a distinction between an "inline function with
external linkage" and an "extern inline function". Is this
intentional?
What about static local variables and string literals of inline
functions
with external linkage that are not explicitly declared extern -- do
they
cause undefined behaviour?

Best Regards,
Vladimir Marko

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jg <jgu222@gmail.com>
Date: Wed, 16 May 2007 23:34:45 CST
Raw View
On May 16, 12:13 pm, Vladimir Marko <swe...@post.sk> wrote:
> Even in the most recent draft (n2284) the paragraph 7.1.2/4 says
>     ... An inline function with external linkage shall have the same
>     address in all translation units. A static local variable in an
> extern
>     inline function always refers to the same object. A string literal
> in
>     an extern inline function is the same object in different
> translation
>     units.
> The wording makes a distinction between an "inline function with
> external linkage" and an "extern inline function". Is this
> intentional?
> What about static local variables and string literals of inline
> functions
> with external linkage that are not explicitly declared extern -- do
> they
> cause undefined behaviour?
>
> Best Regards,
> Vladimir Marko
>
Isn't an inline function with external linkage the same as extern
inline function ?!

JG

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]