Topic: Static functions ?


Author: Michael Rubenstein <miker3@ix.netcom.com>
Date: 1999/09/13
Raw View
On 8 Sep 1999 19:08:46 GMT, Valentin Bonnard
<Bonnard.V@wanadoo.fr> wrote:

>
>Michael Rubenstein wrote:
>
>> You can only have one copy of an "C" function with external
>> linkage no matter what the namespace is.
>
>I understand that extern "C" ``disables'' namespaces; I never
>realized that it applies to unnamed namespaces too, making it
>impossible to have extern "C" ``static'' functions.
>
>> This, of course, also applies to variables,
>
>Does it ?

>From 7.5.1:

 Two declarations for an object with C language linkage
 with the same name (ignoring the namespace names that
 qualify it) that appear in different namespace scopes
 refer to the same object. [Note: because of the one
 definition rule (3.2), only one definition for a function

 or object with C linkage may appear in the program; that
 is, such a function or object must not be defined in more

 than one namespace scope
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/09/13
Raw View
Michael Rubenstein <miker3@ix.netcom.com> writes:

>>Michael Rubenstein wrote:
>>
>>> You can only have one copy of an "C" function with external
>>> linkage no matter what the namespace is.
>>
>>I understand that extern "C" ``disables'' namespaces;

It doesn't. Only external linkage is affected. Scope rules still apply.

namespace N {
    extern "C" int foo();
}

int m = foo();    // error: no declaration for foo
int n = N::foo(); // ok

>>I never
>>realized that it applies to unnamed namespaces too, making it
>>impossible to have extern "C" ``static'' functions.

No, that isn't a problem.

extern "C" {
    static int bar(); // ok
}

You can define different functions called "bar" in different
translation units if they are static, the same as in C. The
linkage specifier says that the function can be called as
a C function, which might be different from the way a C++
function is called.

--
Steve Clamage, stephen.clamage@sun.com


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/09/14
Raw View
Steve Clamage wrote:
>
> Michael Rubenstein <miker3@ix.netcom.com> writes:
>
> >>Michael Rubenstein wrote:
> >>
> >>> You can only have one copy of an "C" function with external
> >>> linkage no matter what the namespace is.
> >>
> >>I understand that extern "C" ``disables'' namespaces;
>
> It doesn't. Only external linkage is affected. Scope rules still apply.
>
> namespace N {
>     extern "C" int foo();
> }
>
> int m = foo();    // error: no declaration for foo
> int n = N::foo(); // ok

But wht about this:

#include <iostream>

namespace N1
{
  extern "C" void foo();
}

namespace N2
{
  extern "C" void foo()
  {
    std::cout << "N2::foo" << std::endl;
  }
}

int main()
{
  N1::foo();
}

I thought this should be legal and print "N2::foo".
Am I missing anything?

>
> >>I never
> >>realized that it applies to unnamed namespaces too, making it
> >>impossible to have extern "C" ``static'' functions.
>
> No, that isn't a problem.
>
> extern "C" {
>     static int bar(); // ok
> }

What I think he meant is that emulating "static" with unnamed
namespace doesn't work correctly for extern "C". That is:

// foo.cc

namespace // unnamed
{
  extern "C" void foo() {}
}

// bar.cc

namespace // unnamed
{
  extern "C" void foo() {}
}

Now linking together both files gives a duplicatre definition.
That is, if static at namespace scope were disallowed by
future standards, there would be no way to make an extern "C"
function static any more.

>
> You can define different functions called "bar" in different
> translation units if they are static, the same as in C. The
> linkage specifier says that the function can be called as
> a C function, which might be different from the way a C++
> function is called.

But the point was static at namespace scope being deprecated (or
rather a possible reason of static not being deprecated for
functions). If a feature is deprecated, you must expect it
to be removed in a future revision of the standard. Removing
namespace scope static functions from the standard would remove
the possibility of extern "C" functions which are only accessible
from one translation unit.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Liam Fitzpatrick" <liam@iName.com>
Date: 1999/09/09
Raw View
Michael Rubenstein <miker3@ix.netcom.com> wrote in message
news:fsTVN39JLi7g89C9s=+7ArX+m=78@4ax.com...
> One sometimes must make a
> function extern "C" so one can pass a pointer to it to a C
> function.

For the purposes of passing as a pointer to a C function, only the type of
the function needs to be extern "C". The name of the function can have "C++"
language linkage.

This example is from the Standard, 7.5 para 4.

  extern "C" typedef void FUNC();
  FUNC f2;  //  the name  f2  has C++ language linkage and the
    //  function's type has C language linkage

So static functions at namespace do not seem needed to solve this problem.

Liam




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/07
Raw View
Michael Rubenstein wrote:
>
> On Sun, 05 Sep 1999 14:26:36 -0500, blargg <nospam@iname.com>
> wrote:
>
> >In article <hbDSN=+b+RD2XmL3YZYSKO+45QCo@4ax.com>, Michael Rubenstein
> ><miker3@ix.netcom.com> wrote:

> >Is there any reason why static functions at namespace scope aren't
> >deprecated, considering that objects are?
>
> I have no idea; Perhaps someone who was involved in the
> standardization can answer this.

I think that the intent was to deprecate both. (I previously
beleived that both were deprecated.)

I don't see any reasons to use static functions at namespace
scope.

--

Valentin Bonnard
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Michael Rubenstein <miker3@ix.netcom.com>
Date: 1999/09/08
Raw View
On 07 Sep 99 22:36:57 GMT, Valentin Bonnard
<Bonnard.V@wanadoo.fr> wrote:

>Michael Rubenstein wrote:
>>
>> On Sun, 05 Sep 1999 14:26:36 -0500, blargg <nospam@iname.com>
>> wrote:
>>
>> >In article <hbDSN=+b+RD2XmL3YZYSKO+45QCo@4ax.com>, Michael Rubenstein
>> ><miker3@ix.netcom.com> wrote:
>
>> >Is there any reason why static functions at namespace scope aren't
>> >deprecated, considering that objects are?
>>
>> I have no idea; Perhaps someone who was involved in the
>> standardization can answer this.
>
>I think that the intent was to deprecate both. (I previously
>beleived that both were deprecated.)
>
>I don't see any reasons to use static functions at namespace
>scope.

I can't believe that the intent was to deprecate both.  That
assumes the committee somehow overlooked that implications of
using the word "object" and the distinction between objects and
functions goes back to the C standard and is well known.

I just thought of a reason, though it isn't very common.

You can only have one copy of an "C" function with external
linkage no matter what the namespace is.  It is incorrect to have

 namespace
 {
   extern "C"
   {
     void f() {}
   }
 }

in one translation unit and

 namespace
 {
   extern "C"
   {
     int f() { return 1; }
   }
 }

in another.   Make the definitions of f() static and there is no
problem (again, as long as these are in different translation
units).

This, of course, also applies to variables, but there isn't any
reason to make a variable extern "C" unless it is accessed by
name by a C function and that would be impossible if the variable
is in an unnamed namespace or is static.

This isn't true for functions.  One sometimes must make a
function extern "C" so one can pass a pointer to it to a C
function.



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Michael Rubenstein <miker3@ix.netcom.com>
Date: 1999/09/06
Raw View
On Sun, 05 Sep 1999 14:26:36 -0500, blargg <nospam@iname.com>
wrote:

>In article <hbDSN=+b+RD2XmL3YZYSKO+45QCo@4ax.com>, Michael Rubenstein
><miker3@ix.netcom.com> wrote:
>
>> On Sun, 05 Sep 1999 10:41:17 -0500, blargg <nospam@iname.com>
>> wrote:
>>
>> >But this is a C++ forum, so it should be said that static objects
>> >(including functions) at namespace scope are deprecated.
>>
>> No it should not be said.  static objects at namespace scope are
>> deprecated.  Functions are not objects and static functions are
>> not deprecated at namespace scope.
>
>Thanks for the clarifications. I have updated my mental notes.
>
>Is there any reason why static functions at namespace scope aren't
>deprecated, considering that objects are?

I have no idea; Perhaps someone who was involved in the
standardization can answer this.  I've added comp.std.c++ to the
newsgroups.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]