将std :: map用作模板模板参数时出错 [英] Error when pass std::map as template template argument

查看:200
本文介绍了将std :: map用作模板模板参数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了这样的函数,其中有一个模板模板类

I defined a function like this, in which there is a template template class

template<typename Key, typename Value, template <typename, typename> class Map>
    struct ForEachOf {
        void operator()(const Map<Key, Value>& map, std::function<void (Key, Value)> func) {
            for(const auto& pair : map) {
                func(pair.first, pair.second);
            }
        }
    };

std::map<int, string> m { {1, "foo"}, {3, "bar"}};
ForEachOf<int, string, std::map> forEachOf;
    forEachOf(m, [](int key, string value) {
        cout << key << value;
    });

但是,以上代码无法编译.错误是这样的:

However, above code is not able to compile. the error is like:

error: template template argument has different template parameters
      than its corresponding template template parameter
    ForEachOf<int, string, std::map> forEachOf;
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:1119:5: note: too many template
      parameters in template template argument
    template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
note: previous template template parameter is here
    template<typename Key, typename Value, template <typename, typename> class Map>

然后如何在此处传递std::map作为模板template参数?

Then How to pass std::map as the template template parameter here?

推荐答案

您的问题是 ::std::map不只接受两个模板参数.

Your problem is that ::std::map does not take only two template parameters.

解决方案只是添加一个仅包含两个参数而不是四个参数的模板:

The solution is simply to add a template that takes only two parameters instead of four:

template<typename key, typename value>
using mymap = std::map<key, value>;

(看到它)

或者,也可以添加缺少的参数及其默认值:

Or, alternatively add the missing arguments with their defaults:

template<typename Key, typename Value, template <typename, typename, typename, typename> class Map>
struct ForEachOf {
    void operator()(const Map<Key, Value, ::std::less<Key>, ::std::allocator<std::pair<const Key, T> >>& map, std::function<void (Key, Value)> func) {
        for(const auto& pair : map) {
            func(pair.first, pair.second);
        }
    }
};

(看到它)

可以类似地通过使用可变参数类型模板来编写:

Which can similarly be written by using a variadic type template:

template<typename Key, typename Value, template <typename...> class Map>
struct ForEachOf {
     void operator()(const Map<Key, Value>& map, std::function<void (Key, Value)> func) {
        for(const auto& pair : map) {
            func(pair.first, pair.second);
        }
    }
};

(看到它)

当然,您也可以只创建一个模板函数来获取地图并推导所有内容:

Of course, you could also just create a template function that takes the map and deduces everything:

#include <map>
#include <string>
#include <iostream>

template<typename T, typename F>
void ForEachOf(T&& map, F&& func) {
    for(auto& pair : map) {
        func(pair.first, pair.second);
    }
}

int main(void) {
    std::map<int, std::string> m { {1, "foo"}, {3, "bar"}};
    ForEachOf(m, [](auto key, auto value) {
        ::std::cout << key << value;
    });
}

(看到它)

这篇关于将std :: map用作模板模板参数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆