2

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!

Anton Pegushin
  • 450
  • 3
  • 11
  • Could you specify what "doesn't work" means? Some kind of error description that makes this question and the answer below findable would make this much more useful. – Ulrich Eckhardt Jul 20 '14 at 09:21
  • @UlrichEckhardt "doesn't work" meant a cryptic template instantiation error that I saw disappear as soon as I added --std=c++0x to the command line. Adding the error message would not make this more easily searchable, I'm afraid. – Anton Pegushin Jul 21 '14 at 05:20
  • It's actually possible to read those errors. All you need to do is to structure them a bit, so that you see that the nested templates of templates that fill one page are just a `map`. Then, everything that's left is the error message that relates to these types, and that sure can be searched. :) – Ulrich Eckhardt Jul 21 '14 at 05:35

1 Answers1

4

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);
}
Community
  • 1
  • 1
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180