【问题标题】:Using C++ template parameter as argument to another template? [duplicate]使用 C++ 模板参数作为另一个模板的参数? [复制]
【发布时间】:2025-12-06 02:15:02
【问题描述】:

我正在尝试编译下面的代码:

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;

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

【问题讨论】:

  • 我认为在模板参数之前需要typename

标签: c++ templates syntax-error


【解决方案1】:

改变这一行:

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

进入:

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

因为std::map&lt;K, V&gt;::iterator 取决于您需要告诉编译器它是一个类型的模板参数。

【讨论】: