smf68
smf68

Reputation: 1008

What does "static" mean in the context of declaring global template functions?

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 */
}

Upvotes: 0

Views: 511

Answers (1)

Bathsheba
Bathsheba

Reputation: 234815

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.

Upvotes: 1

Related Questions