gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:04:00 26 4
gpt4 key购买 nike

我认为这个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com