开发者

can the functors called from algorithms acting on a map accept pair<K, V> instead of value_type?

I tried to write a short function to invert an std::map<K, V> (I know about boost.bimap, this is for self-education), and found, to my surprise, that the code that GCC 4.4 accepted with -pedantic -ansi settings was rejected as const-incorrect by SunCC (5.8, from 2005).

Since value_type is std::pair<const K, V>, SunCC insisted that I const-qualify my K type in the arguments to functions that are passed to transform() and for_each(), and in the type of the return value to be passed to std::inserter, as far as I can tell, it might be right? Which compiler was standards-compliant?

#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <algorithm>
template<typename K, typename V>
std::pair<V, K> flip_pair(const std::pair<K, V>& p) // GCC/MSVC
//std::pair<const V, K> flip_pair(const std::pair<开发者_StackOverflow社区const K, V>& p) // SunCC
{
     return std::make_pair(p.second, p.first); // GCC/MSVC
//     return std::pair<const V, K>(p.second, p.first); // SunCC
}
template<typename K, typename V>
std::multimap<V, K> invert_map(const std::map<K, V>& in)
{
     std::multimap<V, K> out;
     transform(in.begin(), in.end(), std::inserter(out, out.begin()),
               flip_pair<K, V>);
     return out;
}
void print_pair(const std::pair<int, std::string>& p) // GCC/MSVC
//void print_pair(const std::pair<const int, std::string>& p) // SunCC
{
        std::cout << p.first << '\t' << p.second << '\n';
}
int main()
{
     std::map<std::string, int> map;
     map["foo"] = 1; map["bar"] = 2; map["baz"] = 3;
     std::multimap<int, std::string> revmap = invert_map(map);
     for_each(revmap.begin(), revmap.end(), print_pair);
}


Visual C++ and g++ are correct; this code (with flip_pair<K, V>() taking a const std::pair<K, V>&) is okay.

Inside of transform, flip_pair<K, V> is being called. Since the object being passed to that function is a pair<const K, V>, a temporary object of type pair<K, V> is created (pair has a converting constructor that allows you to convert one pair type to another if the .first and .second types are convertible).

This temporary is passed to flip_pair<K, V>(), taking advantage of the fact that a const reference can be bound to a temporary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜