【发布时间】:2015-03-30 07:01:40
【问题描述】:
考虑一个 c++ 模板函数特化:
namespace test {
template < const int L, typename InputIt >
typename std::iterator_traits<InputIt>::value_type
Sum_L( InputIt beg, InputIt end)
{
typedef typename std::iterator_traits<InputIt>::value_type real_t;
for( int i=0 ; i<L; ++i)
call_rearrange_sum( beg, end);
real_t sum( 0 );
for( ; beg != end; ++beg)
sum += *beg;
return sum;
}
template < const int L, typename Real >
Real
Sum_L( const std::size_t len, Real * x)
{
for( int i=0 ; i<L; ++i)
call_rearrange_sum( x, x+len);
Real sum( 0 );
for( std::size_t i=0; i< len; ++i)
sum += x[i];
return sum;
}
template < typename Real, typename Func >
Real
special_sum( std::size_t len, const Real * const x, const Real * y, Func f)
{
std::vector<Real> res( 2*len );
for( std::size_t i=0; i<len; ++i) {
Real tmp;
res.push_back( call_operator( x[i], y[i], &tmp);
res.push_back( tmp );
}
return f( res.begin(), res.end(), f);
}
}
现在我想将上述函数用作:
double my_test( const std::size_t len, double * x, double * y)
{
return test::special_sum( len, x,y,
test::Sum_L< 4, typename std::vector<double>::iterator> );
}
gcc 4.9.2 无法找到正确的模板特化函数。错误是“没有匹配函数调用'special_sum(std::size_t&, const double*&, const double*&, )'。
我知道编译器很难解决。无论我尝试什么,它都是两个模板函数“Sum_L”之一,以获得额外的虚拟模板参数。有没有其他办法?
谢谢。
【问题讨论】:
-
我猜您需要 SFINAE,删除 Sum_L 重载解决方案之一。
标签: c++