Domanda

Qualcuno può spiegarmi come enumerare un BOOST_ENUM utilizzando BOOST_FOREACH?L'esempio seguente mostra che sono riuscito a farlo funzionare con std::for_each, ma non con BOOST_FOREACH.

Codice d'esempio :

BOOST_ENUM_VALUES(  MyEnum, 
  const char *, 
      (xMin)("xMin")
      (xMax)("xMax")
      (yMin)("yMin")
      (yMax)("yMax")
);

void blah(const MyEnum & val)  // working demo with std_foreach
{
  std::cout << "index=" << val.index() << " val=" << val.value() << " str=" << val.str() << std::endl;
}


void foo()
{
  //BOOST_FOREACH : does not compile...
  BOOST_FOREACH(MyEnum myEnum, MyEnum() ) // I tried to construct a "dummy" enum in order to use its iterator with no luck...
  {
    std::cout << "index=" << myEnum.index() << " val=" << myEnum.value() << " str=" << myEnum.str() << std::endl;
  }

  //std::for_each : works...
  std::for_each(MyEnum::begin(), MyEnum::end(), blah);

}

Molte grazie in anticipo!

Modificare:come menzionato nella risposta, il codice funziona con la codebase più recente di boost.

È stato utile?

Soluzione

Il tuo codice di esempio sopra viene compilato e funziona perfettamente per me con gcc 4.5.1 e vc2010 (dopo aver aggiunto i corrispondenti #include, cioè).Ho provato con enum_rev4.6 dal vault.Che errori di compilazione vedi?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top