Boost logo

Boost Users :

From: Tom Matelich (tmatelich_at_[hidden])
Date: 2003-03-26 16:40:06


Compiler: gcc 3.2 and 3.2.2

I believe this is actually a bug in gcc, but I'm going to post it hopes that
someone can help me. We have a handy little tool for generating enums with
associated string conversion and stream insertion/extraction functions.
When we upgraded to boost 1.30.0, lexical_cast'ing one of these enums to
std::string started behaving incorrectly, rather than the name, it is giving
me the integral value. It seems that the lexical_stream is being used.
Unfortunately, my test case is working fine. The complete test case
(including definition of ZAcquisition::PPG) is appended at the end. The
code I am seeing failure with looks like:

void foo(ZAcquisition::PPG::Type gt)
{
...
   std::string str;
   str = boost::lexical_cast<std::string>(gt);
   std::cerr << "DEBUG, storing encode for PPG: " << str << ", aka " << gt
<< std::endl;
...
}

This outputs (for example):
DEBUG, storing encode for PPG: 0, aka ONE

I tried boost::lexical_cast<std::string, ZAcquisition::PPG::Type>(gt) as
well.

Anyone have any ideas? For now I'm going to back out to 1.29.0's
lexical_cast.

Thanks,
Tom

--------Complete Passing Test Case
#include <boost/lexical_cast.hpp>
#include <string>
#include <cassert>
#include <iostream>

namespace ZAcquisition {

namespace PPG {

   enum Type {
                ONE = 0,
                TWO,
                THREE,
                FOUR,
                INVALID = 100
        };

   Type stringToType(std::string const&);
   std::string typeToString(PPG::Type);

   const int size = 5;
   const Type values[size] = { ONE, TWO, THREE, FOUR, INVALID };
}

}
std::ostream& operator<<(std::ostream&, ZAcquisition::PPG::Type);
std::istream& operator>>(std::istream&, ZAcquisition::PPG::Type&);

namespace {
const char s_PPG_ONE[] = "ONE";
const char s_PPG_TWO[] = "TWO";
const char s_PPG_THREE[] = "THREE";
const char s_PPG_FOUR[] = "FOUR";
const char s_PPG_INVALID[] = "INVALID";
}

std::string
ZAcquisition::PPG::typeToString(ZAcquisition::PPG::Type type) {

   std::string ret("Invalid_PPG_type");

   switch(type) {
      case PPG::ONE:
         ret = s_PPG_ONE;
         break;
      case PPG::TWO:
         ret = s_PPG_TWO;
         break;
      case PPG::THREE:
         ret = s_PPG_THREE;
         break;
      case PPG::FOUR:
         ret = s_PPG_FOUR;
         break;
      case PPG::INVALID:
         ret = s_PPG_INVALID;
         break;
      default:
         std::cerr << static_cast<int>(type) << " is not a valid PPG
enumeration." << std::endl;
   }
   return ret;
}

ZAcquisition::PPG::Type
ZAcquisition::PPG::stringToType(std::string const& str) {

   PPG::Type ret = PPG::ONE;

   if(0 == str.compare(s_PPG_TWO)) {
      ret = PPG::TWO;
   } else if(0 == str.compare(s_PPG_THREE)) {
      ret = PPG::THREE;
   } else if(0 == str.compare(s_PPG_FOUR)) {
      ret = PPG::FOUR;
   } else if(0 == str.compare(s_PPG_INVALID)) {
      ret = PPG::INVALID;
   }
   return ret;
}

std::ostream&
operator<<(std::ostream& out, ZAcquisition::PPG::Type type) {
   return out << ZAcquisition::PPG::typeToString(type);
}

std::istream&
operator>>(std::istream& in, ZAcquisition::PPG::Type& type) {
   std::string str;
   in >> str;
   type = ZAcquisition::PPG::stringToType(str);

   return in;
}

int main()
{
   assert(std::string("ONE") ==
boost::lexical_cast<std::string>(ZAcquisition::PPG::ONE));
   return 0;
}

-----------------------------------------------------------------------
DISCLAIMER: Information contained in this message and/or
attachment(s) may contain confidential information of Zetec, Inc.
If you have received this transmission in error, please notify
the sender by return email.
-----------------------------------------------------------------------


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net