I have a TU whose global functions won't be used by any other TUs. I read that declaring them as static
gives them internal linkage, and this is good from an optimization standpoint. But I want to know what are the correct situations in which I should use them. Should I always give global functions/variables internal linkage when I know they won't be used anywhere else in the program?
Asked
Active
Viewed 111 times
1

template boy
- 10,230
- 8
- 61
- 97
-
"and this is good from an optimization standpoint" no. – Cheers and hth. - Alf Sep 30 '14 at 22:14
-
It's possible that your choice of what to do here will affect compile times, but it will be transparent at run time. – iwolf Sep 30 '14 at 22:16
-
@Cheersandhth.-Alf It doesn't help the compiler optimize? – template boy Sep 30 '14 at 22:28
-
1If they are not `static` and not in an unnamed namespace, then you risk a name clash with another TU doing the same thing, which will silently cause undefined behaviour. – M.M Sep 30 '14 at 23:05
1 Answers
7
Put them in an unnamed namespace instead.
This is the idiomatic solution in C++ for functions that will be used only in the current TU.