Trouble with dependent types in templates

假如想象 提交于 2020-01-10 09:34:30

问题


I'm having trouble with templates and dependent types:

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);
    }
}

Compiler complaints, from the location noted above:

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'

What am I doing wrong?


回答1:


Well, the warning says:

dependent name is not a type. prefix with 'typename' to indicate a type

The dependent name (that is, the iterator in std::set<result_t>::iterator) is not a type. You need to prefix it with typename to indicate a type:

typename std::set<result_t>::iterator

So, your declaration should be:

template<class result_t, class Predicate>
set<result_t> findAll_if(typename set<result_t>::iterator begin, typename set<result_t>::iterator end, Predicate pred)
                                                note added typename ^

(and the definition should match the declaration)




回答2:


You need an additional typename keyword on this line:

set<result_t> findAll_if(typename set<result_t>::iterator begin,typenameset<result_t>::iterator end, Predicate pred) // warning C4346



来源:https://stackoverflow.com/questions/2931345/trouble-with-dependent-types-in-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!