Anton Pegushin
Anton Pegushin

Reputation: 470

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

I thought this std::map key extraction into an std::vector should have worked without specifying --std=c++0x flag for gcc (4.6), but it did not. Any idea why?

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

Thanks!

Upvotes: 2

Views: 127

Answers (1)

Daniel Frey
Daniel Frey

Reputation: 56863

The reason is that you are using a local type get_key as the last argument. This was not allowed in C++98 and the rules have been changed/relaxed for C++11.

This can be seen in 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);
}

Upvotes: 3

Related Questions