difference between static const and const at the namespace level

C

cppsks

namespace test
{
static const int num = 10;
const int num2 = 20;
}

What exactly is the difference here? Which is preferred (if both mean the
same)?
 
V

Victor Bazarov

cppsks said:
namespace test
{
static const int num = 10;
const int num2 = 20;
}

What exactly is the difference here? Which is preferred (if both mean the
same)?

There is no difference. I actually prefer no 'static' in this case.

'static' forces internal linkage. Since const objects that don't have
'extern' specifier have internal linkage by default, 'static' become
superfluous in a declaration/definition of a const object.

V
 
A

Andrey Tarasevich

cppsks said:
namespace test
{
static const int num = 10;
const int num2 = 20;
}

What exactly is the difference here?

Absolutely none.
Which is preferred (if both mean the same)?

The second form, probably. It is shorter.

If you were trying to create a header file that is compilable in both C
and C++ code, then maybe it would make sense to use the explicit
'static', since in C constant objects have external linkage by default.
But apparently you are not trying to do that.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top