static function in named namespace

Discussion in 'C++' started by Yuanfang Chen, Oct 22, 2013.

  1. Hi all,

    I am confused by code like this

    namespace foo {

    static void bar() {...}

    }

    what's the purpose of static here?

    best,
    yuanfang
    Yuanfang Chen, Oct 22, 2013
    #1
    1. Advertising

  2. Yuanfang Chen

    Jorgen Grahn Guest

    On Tue, 2013-10-22, Yuanfang Chen wrote:
    > Hi all,
    >
    > I am confused by code like this
    >
    > namespace foo {
    >
    > static void bar() {...}
    >
    > }
    >
    > what's the purpose of static here?


    The same as in the global namespace. What do you think its purpose
    is there?

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Oct 22, 2013
    #2
    1. Advertising

  3. Chris Vine <> wrote:
    > However, this construct is pretty unusual. If bar() has internal
    > linkage, there is usually little point in not having it also in global
    > namespace.


    I think it is actually quite usual. Static functions are usually helper
    functions that are not generic enough to be reused. As such they are often
    defined just before they are used, which is often inside a namespace.

    Tobi
    Tobias Müller, Oct 23, 2013
    #3
  4. Yuanfang Chen

    Jorgen Grahn Guest

    On Wed, 2013-10-23, Tobias Müller wrote:
    > Chris Vine <> wrote:
    >> However, this construct is pretty unusual. If bar() has internal
    >> linkage, there is usually little point in not having it also in global
    >> namespace.

    >
    > I think it is actually quite usual. Static functions are usually helper
    > functions that are not generic enough to be reused. As such they are often
    > defined just before they are used, which is often inside a namespace.


    Different styles I suppose ... I tend to have

    -- header --
    namespace Foo {
    void foo();
    void bar();
    }

    -- source file --
    #include "my_header.h"
    using Foo:foo;
    using Foo:bar;

    namespace {
    // general helpers
    // helpers for foo, if needed
    }

    void foo()
    {
    ...
    }

    namespace {
    // more helpers for bar, if needed
    }

    void bar()
    {
    ...
    }


    So my named namespaces live in the header files, and the source files
    mostly use the global namespace and unnamed ones. I don't have any
    major problems with your way, but I don't want most of my code to be
    indented.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Oct 23, 2013
    #4
  5. Jorgen Grahn <> wrote:

    [...]

    > I don't have any
    > major problems with your way, but I don't want most of my code to be
    > indented.


    I don't indent namespaces anyway. Usually I don't have more than one
    namespace in a file so I don't see any advantage in it.

    Tobi
    Tobias Müller, Oct 23, 2013
    #5
  6. Yuanfang Chen

    Jorgen Grahn Guest

    On Wed, 2013-10-23, Tobias Müller wrote:
    > Jorgen Grahn <> wrote:
    >
    > [...]
    >
    >> I don't have any
    >> major problems with your way, but I don't want most of my code to be
    >> indented.

    >
    > I don't indent namespaces anyway. Usually I don't have more than one
    > namespace in a file so I don't see any advantage in it.


    Yeah, that was something I considered doing for a while. In the end
    it boiled down to "I don't want others to have to fight their editor
    about the indentation" and "I don't want to learn how to train Emacs
    not to indent namespaces".

    (On the other hand I'm not consistent: Emacs wants to indent 'extern
    "C"' blocks and that's clearly crazy from a C perspective.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Oct 25, 2013
    #6
  7. Jorgen Grahn <> wrote:
    > On Wed, 2013-10-23, Tobias Müller wrote:
    >> Jorgen Grahn <> wrote:
    >>
    >> [...]
    >>
    >>> I don't have any
    >>> major problems with your way, but I don't want most of my code to be
    >>> indented.

    >>
    >> I don't indent namespaces anyway. Usually I don't have more than one
    >> namespace in a file so I don't see any advantage in it.

    >
    > Yeah, that was something I considered doing for a while. In the end
    > it boiled down to "I don't want others to have to fight their editor
    > about the indentation" and "I don't want to learn how to train Emacs
    > not to indent namespaces".


    In my experience this is only a problem when inserting something at the
    very beginning of the namespace. At least Visual Studio only considers the
    local context when indenting.
    Except when you autoformat the whole file of course, but I almost never do
    that.

    > (On the other hand I'm not consistent: Emacs wants to indent 'extern
    > "C"' blocks and that's clearly crazy from a C perspective.)


    You mean because of the usual #ifdef __cplusplus guards?
    Conditional compilation like that makes "correct" indenting impossible
    anyway.

    Tobi
    Tobias Müller, Oct 26, 2013
    #7
  8. Yuanfang Chen

    Jorgen Grahn Guest

    On Sat, 2013-10-26, Tobias Müller wrote:
    > Jorgen Grahn <> wrote:
    >> On Wed, 2013-10-23, Tobias Müller wrote:
    >>> Jorgen Grahn <> wrote:
    >>>
    >>> [...]
    >>>
    >>>> I don't have any
    >>>> major problems with your way, but I don't want most of my code to be
    >>>> indented.
    >>>
    >>> I don't indent namespaces anyway. Usually I don't have more than one
    >>> namespace in a file so I don't see any advantage in it.

    >>
    >> Yeah, that was something I considered doing for a while. In the end
    >> it boiled down to "I don't want others to have to fight their editor
    >> about the indentation" and "I don't want to learn how to train Emacs
    >> not to indent namespaces".

    >
    > In my experience this is only a problem when inserting something at the
    > very beginning of the namespace. At least Visual Studio only considers the
    > local context when indenting.
    > Except when you autoformat the whole file of course, but I almost never do
    > that.
    >
    >> (On the other hand I'm not consistent: Emacs wants to indent 'extern
    >> "C"' blocks and that's clearly crazy from a C perspective.)

    >
    > You mean because of the usual #ifdef __cplusplus guards?
    > Conditional compilation like that makes "correct" indenting impossible
    > anyway.


    I meant it's "clearly crazy" because you want to distract C users of
    your header file as little as possible, so they don't petition you to
    drop C++ compatibility ...

    At least with Emacs, I don't think the indenting logic looks at
    #ifdefs at all.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Oct 26, 2013
    #8
  9. Jorgen Grahn <> writes:
    > At least with Emacs, I don't think the indenting logic looks at
    > #ifdefs at all.


    The curly bracket in

    extern "C" {

    makes Emacs indent the following line. But if you manually put the first
    declaration at column zero, Emacs "does what you want" for following
    ones.


    Rupert
    Rupert Swarbrick, Oct 27, 2013
    #9
  10. Jorgen Grahn <> writes:
    > At least with Emacs, I don't think the indenting logic looks at
    > #ifdefs at all.


    The curly bracket in

    extern "C" {

    makes Emacs indent the following line. But if you manually put the first
    declaration at column zero, Emacs "does what you want" for following
    ones.


    Rupert
    Rupert Swarbrick, Oct 27, 2013
    #10
  11. On Wednesday, October 23, 2013 1:14:24 PM UTC-4, Jorgen Grahn wrote:
    > On Wed, 2013-10-23, Tobias Müller wrote:
    >
    > > Chris Vine <> wrote:

    >
    > >> However, this construct is pretty unusual. If bar() has internal

    >
    > >> linkage, there is usually little point in not having it also in global

    >
    > >> namespace.

    >
    > >

    >
    > > I think it is actually quite usual. Static functions are usually helper

    >
    > > functions that are not generic enough to be reused. As such they are often

    >
    > > defined just before they are used, which is often inside a namespace.

    >
    >
    >
    > Different styles I suppose ... I tend to have
    >
    >
    >
    > -- header --
    >
    > namespace Foo {
    >
    > void foo();
    >
    > void bar();
    >
    > }
    >
    >
    >
    > -- source file --
    >
    > #include "my_header.h"
    >
    > using Foo:foo;
    >
    > using Foo:bar;
    >
    >
    >
    > namespace {
    >
    > // general helpers
    >
    > // helpers for foo, if needed
    >
    > }
    >
    >
    >
    > void foo()
    >
    > {
    >
    > ...
    >
    > }
    >
    >
    >
    > namespace {
    >
    > // more helpers for bar, if needed
    >
    > }
    >
    >
    >
    > void bar()
    >
    > {
    >
    > ...
    >
    > }
    >
    >
    >
    >
    >
    > So my named namespaces live in the header files, and the source files
    >
    > mostly use the global namespace and unnamed ones. I don't have any
    >
    > major problems with your way, but I don't want most of my code to be
    >
    > indented.
    >
    >
    >
    > /Jorgen
    >
    >
    >
    > --
    >
    > // Jorgen Grahn <grahn@ Oo o. . .
    >
    > \X/ snipabacken.se> O o .


    Same here.

    I'm confused at first because unnamed/global space is the most common placeI've seen to put stuff with internal linkage. I only put reuse code insidea named space. Now I figured it is a way to put some implementation insideheader file.
    Yuanfang Chen, Oct 29, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    563
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,124
  3. Anonymous
    Replies:
    3
    Views:
    529
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    448
    Woebegone
    Nov 19, 2004
  5. Replies:
    2
    Views:
    514
    Jonathan Mcdougall
    Aug 6, 2007
Loading...

Share This Page