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

标签 c++ c++11 g++

我认为将 std::map 键提取到 std::vector 应该可以在不为 gcc (4.6) 指定 --std=c++0x 标志的情况下工作,但事实并非如此。知道为什么吗?

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 的规则已更改/放宽。

这可以在 this example 中看到:

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);
}

关于c++ - 为什么这个 std::map 键提取函数需要 --std=c++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848319/

相关文章:

c++ - Zeromq:如何在 C++ 中访问 tcp 消息

c++ - 如何为函数(C++)声明字符序列?

c++ - 在不知道其元数的情况下绑定(bind)函数的第一个参数

c++ - unordered_map - {{key,value},{key,value}} 语法无效

c++ - 使用 std::vector::emplace_back

c++ - 如何为 jsoncpp 编写 cmake 模块?

c++ - 是否有 itoa 版本返回写入缓冲区的字符数?

c++ - 动态子对象内存分配 Clang++ vc g++

c++ - 虚拟方法上的 g++ [[noreturn]]

c++ - 对 C/C++ 开发人员的人工支持