Anonymous namespaces and static linkage

Anonymous namespaces and static linkage

Post by Sumanth Vep » Mon, 16 Oct 2000 04:00:00



I was wondering what the difference between the following
C++ constructs was.

namespace {
  bool my_function()
  {
    return true;
  }

Quote:}

and

static bool my_function()
{
  return true;

Quote:}

Do both have the same linkage? Can the address of either
function be passed back as a return value by another
function (with external linkage) within the same translation
unit ?


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Anthony William » Tue, 17 Oct 2000 04:00:00


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Quote:> I was wondering what the difference between the following
> C++ constructs was.

> namespace {
>   bool my_function()
>   {
>     return true;
>   }
> }

> and

> static bool my_function()
> {
>   return true;
> }

> Do both have the same linkage?

No. The function in the unnamed namespace has external linkage, and the
static
function has internal linkage.

Quote:> Can the address of either
> function be passed back as a return value by another
> function (with external linkage) within the same translation
> unit ?

Yes.

The only real difference is that the static function cannot be used anywhere
something with external linkage is required, such as for a template non-type
parameter:

template<bool (*func)()>
class C
{

Quote:};

C<my_function> is permitted for the anonymous namespace function, and not
for
the static function.

Anthony
- --


http://anthonyw.cjb.net/ -- ALINK home page
PGP Fingerprint:
0E2D D32A 8732 DC31 804C  D435 9BF0 F8FE 1C1B 9AD5
PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.1 for non-commercial use <http://www.pgp.com>
Comment: PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc

iQA/AwUBOeq8SZvw+P4cG5rVEQJuQwCeO7s4Q1bJnEEEFMH9q+HBCzFw+0cAoNP8
hrLAHMqG9UJ+fz47zS7zX4Qq
=pwTE
-----END PGP SIGNATURE-----


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by C. M. Hear » Tue, 17 Oct 2000 04:00:00


Quote:Sumanth Vepa writes:
> I was wondering what the difference between the following
> C++ constructs was.

> namespace {
>   bool my_function()
>   {
>     return true;
>   }
> }

This function has external linkage, but it does not have a name
which allows it to be referenced outside its translation unit by
a program which conforms to the C++ standard.  It can, however,
be used as a template argument.

Quote:> and

> static bool my_function()
> {
>   return true;
> }

This function has internal linkage.  It cannot be used as a template argument.

Quote:> Do both have the same linkage?

No.

Quote:> Can the address of either function be passed back as a return value
> by another function (with external linkage) within the same translation
> unit ?

Yes.

//cmh


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Greg Come » Tue, 17 Oct 2000 04:00:00




Quote:>I was wondering what the difference between the following
>C++ constructs was.

>namespace {
>  bool my_function()
>  {
>    return true;
>  }
>}

>and

>static bool my_function()
>{
>  return true;
>}

Check out http://www.comeaucomputing.com/techtalk/#nostatic

Quote:>Do both have the same linkage?

No.

Quote:>Can the address of either
>function be passed back as a return value by another
>function (with external linkage) within the same translation
>unit ?

Yes.

- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Jack Klei » Tue, 17 Oct 2000 04:00:00



in comp.lang.c++.moderated:

Quote:> I was wondering what the difference between the following
> C++ constructs was.

> namespace {
>   bool my_function()
>   {
>     return true;
>   }
> }

This is the preferred method.

Quote:> and

> static bool my_function()
> {
>   return true;
> }

This is legal, but deprecated (i.e., not recommended and may be
removed from the language by a future version of the standard).

Quote:> Do both have the same linkage? Can the address of either
> function be passed back as a return value by another
> function (with external linkage) within the same translation
> unit ?

The static version has internal linkage.  If the unnamed namespace
version is defined at file scope (that is, not inside another
namespace), it does as well.  If the unnamed namespace is nested
inside another, named namespace, I think, but am not sure, its scope
is restricted to the outer namespace.

In any case a pointer to the function can be passed as a parameter in
a call or as a return value, to code where the function is not
accessible by name, and that code can use the function pointer to call
the function.

Jack Klein
--
Home: http://jackklein.home.att.net


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by C. M. Hear » Wed, 18 Oct 2000 04:00:00


[ ... ]

Quote:> > static bool my_function()
> > {
> >   return true;
> > }

> This is legal, but deprecated (i.e., not recommended and may be
> removed from the language by a future version of the standard).

Not true.  The standard deprecates static namespace scope
_objects_, but not static namespace scope _functions_.

//cmh


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Greg Come » Wed, 18 Oct 2000 04:00:00





>[ ... ]
>> > static bool my_function()
>> > {
>> >   return true;
>> > }

>> This is legal, but deprecated (i.e., not recommended and may be
>> removed from the language by a future version of the standard).

>Not true.  The standard deprecates static namespace scope
>_objects_, but not static namespace scope _functions_.

Technically yes, but when the meaning of deprecated is re-considered,
it many very well be.  That said, another possibility is that
static namespace scope objects won't be deprecated.  See:

    http://www.comeaucomputing.com/iso/cwg_closed.html#167
    http://www.comeaucomputing.com/iso/cwg_closed.html#174
    http://www.comeaucomputing.com/iso/cwg_active.html#223

- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Christopher Eltschk » Sat, 21 Oct 2000 04:00:00






> >[ ... ]
> >> > static bool my_function()
> >> > {
> >> >   return true;
> >> > }

> >> This is legal, but deprecated (i.e., not recommended and may be
> >> removed from the language by a future version of the standard).

> >Not true.  The standard deprecates static namespace scope
> >_objects_, but not static namespace scope _functions_.

> Technically yes, but when the meaning of deprecated is re-considered,
> it many very well be.  That said, another possibility is that
> static namespace scope objects won't be deprecated.  See:

>     http://www.comeaucomputing.com/iso/cwg_closed.html#167
>     http://www.comeaucomputing.com/iso/cwg_closed.html#174
>     http://www.comeaucomputing.com/iso/cwg_active.html#223

Given that extern "C" functions don't respect namespaces during
linking, you cannot replace static functions with C linkage with
anonymous namespace versions:

extern "C"
{
  static void f() {} // effectively local to this translation unit

Quote:};

namespace
{
  extern "C" void f() {}
     // not local, every declaration of extern "C" void f()
     // elsewhere in the program (especially in other translation
     // units) refers to the same function. Every other definition
     // of an extern "C" void f() (including those in unnamed
     // namespaces) will be a violation of the ODR.

Quote:}

But that brings up a question: consider:

extern "C"
{
  typedef void c_func();

Quote:}

namespace ns1
{
  cfunc f();

Quote:}

namespace ns2
{
  cfunc f();

Quote:}

The question now is: Are ns1::f() and ns2::f() the same function
(as if they would be if they had been declared with extern "C"
directly), or are they two different, unrelated functions which
just happen to both use C calling conventions?

If they are two unrelated functions, then static functions
with C linkage can be emulated with unnamed namespaces, and
deprecating static functions as well maight make sense. OTOH,
if they behave exactly like normal extern "C" functions (i.e.
they are the same function), then static is still needed to
allow file-local functions to be passed to C APIs.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Ivan Veceri » Sun, 22 Oct 2000 04:00:00


Christopher Eltschka said:

Quote:>Given that extern "C" functions don't respect namespaces during
>linking, you cannot replace static functions with C linkage with
>anonymous namespace versions:

>extern "C"
>{
>  static void f() {} // effectively local to this translation unit
>};

Isn't there a contradiction in the terms:
 "f() is a function whose linkage is _local_ to this translation
  unit, but uses "C" conventions for _external_ linkage."

There is no external linkage.
So you can drop the extern "C".
So you can use an anonymous namespace instead of static.

Well, I guess that someone could find a way to include some old C
source file within a C++ file, to demonstrate that such a construct
could occur. But in any case, 'static' global definitions are
really only there for backwards compatibility with C,
and their use should be considered as deprecated.

Regards,
Ivan

---------------------------------------------------------
 Ivan Vecerina, Dr. med.
 Senior Software Developer - Surgical Navigation Network
  <http://www.surgnav.com> - <http://www.cedara.com>


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Anonymous namespaces and static linkage

Post by Christopher Eltschk » Thu, 26 Oct 2000 04:00:00



> Christopher Eltschka said:
> >Given that extern "C" functions don't respect namespaces during
> >linking, you cannot replace static functions with C linkage with
> >anonymous namespace versions:

> >extern "C"
> >{
> >  static void f() {} // effectively local to this translation unit
> >};

> Isn't there a contradiction in the terms:
>  "f() is a function whose linkage is _local_ to this translation
>   unit, but uses "C" conventions for _external_ linkage."

> There is no external linkage.
> So you can drop the extern "C".
> So you can use an anonymous namespace instead of static.

So how do you suppose that function to be passed via pointer to
a C function if it is not declared as having C linkage?
Note that, while many compilers allow that, in standard C++
you may not assign a pointer to a function with C++ linkage
(like the one you get if you take the address of a function
without extern "C") to a pointer to a function with C linkage
(as C APIs expect). That is, given the following two definitions

  typedef void (* pcpp)();
  extern "C" typedef void (* pc)();

the types pcpp and pc are different, and assignment from one
to the other are not allowed. Taking the address of a function
declared extern "C" gives a pc, taking the address of a function
not declared extern "C" gives a pcpp.

Quote:>From CD2, 7.5 [dcl.link]/1:

   Two function types with different language linkages are distinct
   types even if they are otherwise identical.

However, I've found the following in CD2, 7.5 [dcl.link]/1:

   All function types, function names, and variable names have
   a language linkage.

and the following in CD2, 7.5 [dcl.link]/4:

  [Example:

          extern "C" void f1(void(*pf)(int));
                          // the name f1 and its function type have C
language
                          // linkage; pf is a pointer to a C function
          extern "C" typedef void FUNC();
          FUNC f2;        // the name f2 has C++ language linkage and
the
                          // function's type has C language linkage
          extern "C" FUNC f3;
                          // the name of function f3 and the function's
type
                          // have C language linkage
          void (*pf2)(FUNC*);
                          // the name of the variable pf2 has C++
linkage and
                          // the type of pf2 is pointer to C++ function
that
                          // takes one parameter of type pointer to C
function
   --end example]

Unless this changed for the final standard (which I don't assume,
since it makes sense to me), the standard obviously distinguishes
between C linkage names and C linkage types. However, in
CD2, 7.5 [dcl.link]/6 it is not very clear if "a function with
C language linkage" is a function with a C linkage name, or a
function with a C linkage type. Since this is the section which
demands that all functions with C linkage shall be the same
function, if the name is the same, it's not unimportant if that
refers to the name or to the type.
IMHO the interpretation which makes most sense it that it refers
to the function name linkage (since that is what causes the
linker to treat them as the same function). If so, it's possible
(with the typedef trick) to get an extern "C" type function
which isn't identical to other extern "C" type functions with
the same name. If that interpretation is correct and still holds
with the real standard, but *only* then, static functions should
IMHO be deprecated.
(Besides, if the standard is still not more explicit in
7.5 [dcl.link]/6, I guess a clarification would be quite
a good idea.)

[...]


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

1. namespaces with static linkage?

Section 3.5, [basic.link]/4, says

I assume this talks about code like

namespace{
  namespace ns{
  }

and defines that ns has no linkage. Why is this restriction important?
IOW, what would change if the 'unless ...'  part is omitted?

TIA,
Martin

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

2. Aspect Oriented Programming

3. Will all anonymous namespace variables act as static ?

4. Com Port

5. static and anonymous namespace

6. Alarm sounds

7. Anonymous namespace vs. static variables

8. VCD making questions

9. Namespace and Linkage

10. DLL linkage error of static method

11. static linkage?

12. anonymous namespace and symbols in global scope ?

13. How to define (not declare) a static constant with external linkage?