Topic: static - internal linkage


Author: tmartsum@gmail.com
Date: Mon, 26 Sep 2005 08:53:50 CST
Raw View
I was in a discussion on lang.com.c++ where I argued NOT to use static
i front of global variable....

(In C and older C++ programs the keyword static is (confusingly) used
to
mean "use internal linkage" (   B.2.3) Don't use static except inside
function (   7.1.2) and classes (   10.2.4)
from C++ programming language section 9.2.1
by Bjarne Stroustrup)

However somebody claimed that things had changed (or would soon) to
mean "use internal linkage". Is that true ?


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Tony" <gottlobfrege@gmail.com>
Date: 26 Sep 2005 20:10:09 GMT
Raw View
tmartsum@gmail.com wrote:
> I was in a discussion on lang.com.c++ where I argued NOT to use static
> i front of global variable....
>
> (In C and older C++ programs the keyword static is (confusingly) used
> to
> mean "use internal linkage" (   B.2.3) Don't use static except inside
> function (   7.1.2) and classes (   10.2.4)
> from C++ programming language section 9.2.1
> by Bjarne Stroustrup)
>
> However somebody claimed that things had changed (or would soon) to
> mean "use internal linkage". Is that true ?
>
>

static has always, and still does, mean "use internal linkage".  Many
people favour using unnamed namespaces instead, but I find static still
useful, because without static, most (all?) compilers still expose the
objects for external linkage (even though they will never be found) and
thus slow down your linking (although only noticeable on large
projects).

eg:

namespace
{
   int foo;
   int bar();

   class Class { };
}

'foo' and 'bar' still have external linkage, but with a mangled-name
that includes the unique name of the unnamed namespace.  So they
unnecessarily increase the size of the linker's symbol dictionary,
slowing down lookups and linking.

Instead use both 'static' and the namespace:

namespace
{
   static int foo;
   static int bar();

   class Class { };
}

Of course, 'static' can't be used on the class, which makes the
namespace still useful...


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]