static function in named namespace

Y

Yuanfang Chen

Hi all,

I am confused by code like this

namespace foo {

static void bar() {...}

}

what's the purpose of static here?

best,
yuanfang
 
J

Jorgen Grahn

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
 
T

Tobias Müller

Chris Vine said:
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
 
J

Jorgen Grahn

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
 
T

Tobias Müller

[...]
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
 
J

Jorgen Grahn

[...]
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
 
T

Tobias Müller

Jorgen Grahn said:
[...]
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
 
J

Jorgen Grahn

Jorgen Grahn said:
[...]

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
 
R

Rupert Swarbrick

Jorgen Grahn said:
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
 
R

Rupert Swarbrick

Jorgen Grahn said:
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
 
Y

Yuanfang Chen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top