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?

delimy
  • 23
  • 2
  • It does the same thing as normal, non-templated `static` functions in headers: http://stackoverflow.com/questions/780730/c-c-static-function-in-header-file-what-does-it-mean – wkl Dec 03 '10 at 18:35
  • Since its almost certain to become an inlined function I'd say 'not alot' ;) – MerickOWA Dec 03 '10 at 18:39
  • static in this context has been deprecated in favor of an anonymous namespace. – Martin York Dec 03 '10 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). – Martin York Dec 03 '10 at 18:42
  • @Martin: In case of templates, `inline` should not be needed. Templates are always implicitly inline? – UncleBens Dec 03 '10 at 20:44

3 Answers3

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?

Community
  • 1
  • 1
Leo Davidson
  • 6,093
  • 1
  • 27
  • 29
  • 1
    private as in the compilation unit will not expose the symbol name for the linker to resolve dependencies with. – Martin York Dec 03 '10 at 18:41
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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
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.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125