Reputation: 3800
I'm building a simple text analyzer. I have the following code:
template<typename FwdIt0, typename FwdIt1, typename Comp, typename Num>
Num SmartAnalyzer::count_intersection(FwdIt0 beg0, FwdIt0 end0, FwdIt1 beg1, FwdIt1 end1, Comp less, Num n)
{
while (beg0 != end0 && beg1 != end1)
{
if (less(*beg0, *beg1)) ++beg0;
else if (less(*beg1, *beg0)) ++beg1;
else
{
++n;
++beg0;
++beg1;
}
}
return n;
}
// Finds intersection between 2 sentences and divide it to the average sentence length
template<typename FwdIt0, typename FwdIt1>
double SmartAnalyzer::intersection_weight(FwdIt0 beg0, FwdIt0 end0, FwdIt1 beg1, FwdIt1 end1)
{
double const mid_size = 0.5 * (std::distance(beg0, end0) + std::distance(beg1, end1));
/* LINE 38 */ double const intsc = count_intersection(beg0, end0, beg1, end1, std::less<>(), double());
return intsc / mid_size;
}
When I try to compile it, I'm getting:
.. / lib / analyzer.cpp: In member function ‘double SmartAnalyzer::intersection_weight(FwdIt0, FwdIt0, FwdIt1, FwdIt1)’ :
.. / lib / analyzer.cpp : 38 : 76 : error : wrong number of template arguments(0, should be 1)
With GCC 4.7 on Debian
. I think this is somehow related with that GCC until 4.7+ doesn't support template alias, but I don't have any idea how I can fix it. I don't have the opportunity to update gcc to 4.8.
Upvotes: 0
Views: 5015
Reputation: 5102
This one worked for me (gcc 4.8)
As I indicated in the comment, the problem is that you were missing the template parameter for std::less
(note that it seems that on C++14 less gets a specialization for void parameter)
I added a new template parameter X
#include <functional>
#include <iterator>
#include <vector>
template<typename FwdIt0, typename FwdIt1, typename Comp, typename Num>
Num count_intersection(FwdIt0 beg0, FwdIt0 end0, FwdIt1 beg1, FwdIt1 end1, Comp less, Num n)
{
while (beg0 != end0 && beg1 != end1)
{
if (less(*beg0, *beg1)) ++beg0;
else if (less(*beg1, *beg0)) ++beg1;
else
{
++n;
++beg0;
++beg1;
}
}
return n;
}
// Finds intersection between 2 sentences and divide it to the average sentence length
template<typename FwdIt0, typename FwdIt1,typename X>
double intersection_weight(FwdIt0 beg0, FwdIt0 end0, FwdIt1 beg1, FwdIt1 end1)
{
double const mid_size = 0.5 * (std::distance(beg0, end0) + std::distance(beg1, end1));
/* LINE 38 */ double const intsc = count_intersection(beg0, end0, beg1, end1, std::less<X>(), double());
return intsc / mid_size;
}
int main() {
std::vector<int> a;
std::vector<int> b;
intersection_weight<std::vector<int>::iterator,std::vector<int>::iterator,int>(a.begin(),a.end(),b.begin(),b.end());
}
Upvotes: 1
Reputation: 2816
std::less
type must be provided. However it does not work on heterogeneous types (it will in C++14). So in your case, you can't have different value_type
for both FwdIt0
and FWdIt1
.
Maybe you could try the following (when the above point would be solved):
template<typename FwdIt0, typename FwdIt1>
double SmartAnalyzer::intersection_weight(FwdIt0 beg0, FwdIt0 end0, FwdIt1 beg1, FwdIt1 end1)
{
typedef std::iterator_traits<FwdIt0>::value_type less_type;
double const mid_size = 0.5 * (std::distance(beg0, end0) + std::distance(beg1, end1));
/* LINE 38 */ double const intsc = count_intersection(beg0, end0, beg1, end1, std::less<less_type>(), double());
return intsc / mid_size;
}
Upvotes: 1