使用C ++模板参数作为另一个模板的参数?

时间:2014-03-12 14:57:39

标签: c++ templates syntax-error

我正在尝试使用以下代码进行编译:

template <typename K, typename V>
static void addMapping(const K& id, const V& index, std::map<K, V>& mapset) 
{
    std::pair< std::map<K, V>::iterator, bool > ret;
    // ...
}

但是我收到以下错误消息:

error: type/value mismatch at argument 1 in template parameter list for ‘template<class _T1, class _T2> struct std::pair’
     std::pair< std::map<K, V>::iterator, bool > ret;

我记得当你想使用模板参数作为另一个模板的参数时,你需要写一些特别的东西,但我不记得那是什么......

1 个答案:

答案 0 :(得分:4)

更改此行:

std::pair< std::map<K, V>::iterator, bool > ret;

成:

std::pair< typename std::map<K, V>::iterator, bool > ret;

由于std::map<K, V>::iterator依赖于模板参数,因此需要告诉编译器它是一种类型。