【发布时间】:2015-04-07 15:46:41
【问题描述】:
我正在阅读 Alex Graves 的 rnnlib。
在他的代码中,有很多静态函数模板不是类成员方法,而是定义为static,而有些则不是。(见下文)
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);
}
}
...
为什么有些全局函数模板定义为静态,而有些则非?
【问题讨论】:
-
单一定义规则?
-
见:stackoverflow.com/questions/8406800/… PS。我认为您的问题与模板无关,而与静态修饰符无关...
-
@KerrekSB:这些函数已经内联,所以不会破坏 ODR。