2

code from google's v8 js engine, allocation.h:

 template <typename T> static void DeleteArray(T* array) { delete[] array; } 

This is a function template (top level, not in any class). But what the static keyword is for?

5
  • It does the same thing as normal, non-templated static functions in headers: stackoverflow.com/questions/780730/… Commented Dec 3, 2010 at 18:35
  • Since its almost certain to become an inlined function I'd say 'not alot' ;) Commented Dec 3, 2010 at 18:39
  • static in this context has been deprecated in favor of an anonymous namespace. Commented Dec 3, 2010 at 18:39
  • 1
    Personally I regard that as an error (because it is in the header file). It should probably by marked inline rather than static. In this context inline lets the compiler know that there mya be multiple versions of a function in different compilation units and thus it should pick one at link time (if some have not actually been physically inlined). Commented Dec 3, 2010 at 18:42
  • @Martin: In case of templates, inline should not be needed. Templates are always implicitly inline? Commented Dec 3, 2010 at 20:44

3 Answers 3

2

That it's a template is actually a side-issue.

It being static means the function is not visible outside of the file (but since it's in a header and headers are effectively part of the files that include them, that means outside of the file that includes the header; and each file that includes the header effectively has its own, identical but private version of the function).

From http://msdn.microsoft.com/en-us/library/s1sb61xd%28VS.80%29.aspx:

"When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared)."

See here fore more on what it means to have this in a header file:

C/C++: Static function in header file, what does it mean?

Sign up to request clarification or add additional context in comments.

1 Comment

private as in the compilation unit will not expose the symbol name for the linker to resolve dependencies with.
1

The static keyword gives the definition "internal linkage", which means it would be legal to give the name DeleteArray another meaning or a different definition in a different source file. (Just as is the case with static void f(); or static int i;.) But I can't imagine anyone would want to do that.

Use of static this way in C++ is deprecated. This declaration would probably be better without the static keyword, making it implicitly extern (and still inline). In that case, the linker would be allowed to combine any definitions of DeleteArray<T>(T*) for the same T from multiple objects, since they would be the same thing.

Comments

0

static functions (outside of a class) cannot be used outside the defining file. It's a holdover from C where there are no namespaces. I bet you'll find that DeleteArray() isn't called from another file, unless it's been redefined in that other file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.