为什么这个std::map键提取函数需要——std=c++

Why does this std::map keys extraction function need --std=c++?

本文关键字:std c++ 函数 map 为什么 提取      更新时间:2023-10-16

我认为在没有为gcc(4.6)指定——std=c++0x标志的情况下,std::map键提取到std::vector应该可以工作,但是它没有。知道为什么吗?

template <typename Map, typename Container>
void extract_map_keys(const Map& m, Container& c) {
    struct get_key {
        typename Map::key_type operator()
            (const typename Map::value_type& p) const {
                return p.first;
        }
    };
    transform(m.begin(), m.end(), back_inserter(c), get_key());
}

谢谢!

原因是您使用本地类型get_key作为最后一个参数。这在c++ 98中是不允许的,并且在c++ 11中已经更改/放宽了规则。

template <class T> bool cpp0X(T)  {return true;} //cannot be called with local types in C++03
                   bool cpp0X(...){return false;}
bool isCpp0x() 
{
   struct local {} var;
   return cpp0X(var);
}