3

I'm reading Alex Graves's rnnlib. In his codes there are many static function templates which are not class member methods but defined as static, while some are not.(see below)

Some code snipplets of Helpers.hpp:

...
// static
template <class R> static void sort(R& r)  
{
    sort(boost::begin(r), boost::end(r));
}
// static
template <class R> static void reverse_sort(R& r) 
{
    sort(boost::rbegin(r), boost::rend(r));
}
// non static
template <class R> pair<typename range_value<R>::type, typename range_value<R>::type> minmax(const R& r) 
{
    pair<typename range_const_iterator<R>::type, typename range_const_iterator<R>::type> p = minmax_element(boost::begin(r), boost::end(r));
    return make_pair(*p.first, *p.second); 
}
// static
template <class R> static void bound_range (R& r, const typename boost::range_value<R>::type& minVal, const typename boost::range_value<R>::type& maxVal)
{
    for (typename range_iterator<R>::type it = boost::begin(r); it != boost::end(r); ++it) 
    {
        *it = bound(*it, minVal, maxVal);
    }
}
...

Why are some of these global function templates defined as static while some are not?

nn0p
  • 1,189
  • 12
  • 28
  • One-definition rule? – Kerrek SB Apr 07 '15 at 15:48
  • See: http://stackoverflow.com/questions/8406800/when-should-i-write-the-keyword-static-before-a-non-member-function PS. I think your question has nothing to do with templates rather than a static modificator... – W.F. Apr 07 '15 at 15:49
  • @KerrekSB: These functions are already inline, so doesn't break ODR. – Jarod42 Apr 07 '15 at 16:02

2 Answers2

6

In that context, the static keyword refers to static linkage, i.e. that function will be visible only in the translation unit where it's defined.

Now, being that functions defined in a header file, the effect of the static keyword will be that the compiler will generate code for the function in every translation unit that include the header file (and that actually uses that function). The function, furthermore, will be inlined.

For template functions, I'd say that using static, inline or no keyword produce the same result; in fact, in all cases the function will be inlined and no multiple definition error will be raised.

So, the interesting question could be "Why to use static on non-template functions".

Paolo M
  • 12,403
  • 6
  • 52
  • 73
  • What about the non-static function `minmax`? Is it any special? – nn0p Apr 07 '15 at 16:26
  • @nn0p As I said, the result is pretty the same. The "special" is that, would minmax have been a non-template function, you would have been forced to add "static" or "inline" to the signature. – Paolo M Apr 07 '15 at 18:02
-3

Using of static function is when our code compiles then static code will be executed. So linkage of all static functions are done at compile time.

  • 1
    Your answer seems wrong and nonsensical to me. 1) static functions are not evaluated during compile time. Only constant expressions and [constexpr](http://en.cppreference.com/w/cpp/language/constexpr) functions are evaluated during compile time. 2) All functions are linked at compile time (when compiling a binary). May you missed the term static/dynamic linkage with a term static/dynamic binding (this one is about virtual functions)? – Oleg Lokshyn Oct 30 '17 at 11:32