0

I know what static means in the context of declaring global non-template functions (see e.g. What is a "static" function?), which is useful if you write a helper function in a header that is included from several different locations and want to avoid "duplicate definition" errors.

So my question is: What does static mean in the context of declaring global template functions? Please note that I'm specifically asking about global, non-member template functions that do not belong to a class.

In other words, what is the difference between the following two:

template <typename T>
void foo(T t)
{
    /* implementation of foo here */
}

template <typename T>
static void bar(T t)
{
    /* implementation of bar here */
}
Community
  • 1
  • 1
smf68
  • 988
  • 1
  • 9
  • 16

1 Answers1

1

Note that a template function is not actually 'compiled' unless an instance of that template function is required.

Then, that instance has the same properties as a non-template static function: i.e. that instance emanating from its corresponding compilation unit will be invisible to other compilation units, including the linker.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Interesting - does that mean that *static* has no effect here? Or does it change anything? Are the two functions even both correct / valid C++? – smf68 Jun 13 '14 at 09:25
  • 2
    Template functions are automatically inlined. In C++, inline not only hints compiler to inline, but also forbids its inclusion to symbols table. Inline in C++ have about the same meaning as C99's `static inline`. So, here should be no difference. – keltar Jun 13 '14 at 09:38