Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

c++ - Does declaring functions in anonymous namespace with "static" reduces the linking time and memory by not polluting the symbol table

I am in a discussion in one of the code review sessions.

The debate is about if the functions in the anonymous namespace should be declared static or not. As an example, I am being told the square3 function which declared with "static" keyword has advantages.

namespace {
int square2(int num) {
    return num * num;
}

static int square3(int num) { 
    return num * num;
}

}

I have told by my colleagues static is beneficial because:

A static function does not require an entry in the function table and thus does not require time or memory when linking.

static means it is not exposed when linking. It improves linking time and reduces memory usage when linking. Anonymous namespace does not provide this.

I think by "function table" my colleagues mean "symbol table". I couldn't find any documentation about that except the least upvoted answer of another S.O question which says

It is usually better to prefer static linkage, as that doesn't pollute the symbol table

I am trying to see if that info is true or not by using compiler explorer. But I couldn't find a way to get to the symbol table. All I see the function names and everything is mangled in the same way.

So I have two questions:

1 - Is using a function with static keyword in the anonymous namespace helps with memory/linking time?

2 - Is there a way to check the symbol table in compiler explorer?

question from:https://stackoverflow.com/questions/65952213/does-declaring-functions-in-anonymous-namespace-with-static-reduces-the-linkin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1 - Is using a function with static keyword in the anonymous namespace helps with memory/linking time?

Static does not affect functions in anonymous namespace as far as the language is concerned. This is because being in an anonymous namespace already achieves the same thing as declaring the function static achieves. (Note that declaring a member function static has an entirely different meaning).

2 - Is there a way to check the symbol table in compiler explorer?

I don't know of compiler explorer, but you can use for example the nm program to list symbols: http://coliru.stacked-crooked.com/a/0281bc487044ec02

namespace {

       void foo1(){} // internal linkage
static void foo2(){} // internal linkage

}

       void foo3(){} // external linkage
static void foo4(){} // internal linkage

command:

nm main.o | c++filt

output:

0000000000000000 T foo3()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...