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

标签 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;

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

最佳答案

改变这一行:

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

进入:

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

作为std::map<K, V>::iterator取决于您需要告诉编译器它是一种类型的模板参数。

关于c++ - 使用 C++ 模板参数作为另一个模板的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22355398/

相关文章:

c++ - 具有两个以上参数的 Variadic 模板函数

c++ - 使用模板化类的 pimpl 习语有什么优势吗?

php - mysql错误 'TYPE=MyISAM'

c++ - "Implicit instantiation of undefined template"前向声明模板类时

c++ - 为什么 GCC 允许私有(private)嵌套模板类/结构从全局模板函数中可见?

r - R中二进制运算的非数字参数错误,需要说明

python - 显示菜单的python程序中的语法错误

c++ - 为什么这个 std::find 不能比较这些对象

C++ |只有引用变量在函数模板中起作用

c++ - STL 对嵌套类排序