【发布时间】:2011-02-25 06:11:07
【问题描述】:
我在使用模板和依赖类型时遇到问题:
namespace Utils
{
void PrintLine(const string& line, int tabLevel = 0);
string getTabs(int tabLevel);
template<class result_t, class Predicate>
set<result_t> findAll_if(typename set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred) // warning C4346
{
set<result_t> result;
return findAll_if_rec(begin, end, pred, result);
}
}
namespace detail
{
template<class result_t, class Predicate>
set<result_t> findAll_if_rec(set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred, set<result_t> result)
{
typename set<result_t>::iterator nextResultElem = find_if(begin, end, pred);
if (nextResultElem == end)
{
return result;
}
result.add(*nextResultElem);
return findAll_if_rec(++nextResultElem, end, pred, result);
}
}
编译器投诉,来自上述位置:
warning C4346: 'std::set<result_t>::iterator' : dependent name is not a type. prefix with 'typename' to indicate a type
error C2061: syntax error : identifier 'iterator'
我做错了什么?
【问题讨论】:
-
在 VS2017 中,现在是 C7510。
-
如果您使用 Re-sharper 代码问题,它(可能)会产生此警告:
Dependent type requires using 'typename' keyword -
对不起,我不明白为什么这里没有原始代码而是更正后的代码?