Search code examples
c++boostboost-fusion

How to check result from boost::fusion:at_key?


Example from boost tutorial:

using namespace fields;
std::string person_name = at_key<name>(a_person);
int person_age = at_key<age>(a_person);

How to check wheather there is no such key in this map?


Solution

  • To check if a given sequence contains an element associated with a given Key, one can (and should) use has_key<Key> (seq), as in the below example.

    if (has_key<fields::age> (some_seq)) {
      int person_age = at_key<fields::age> (some_seq);
    }
    

    Documentation: