boost :: lexical_cast и строковая обработка не встроенных типов - PullRequest
4 голосов
/ 13 декабря 2011

У меня есть (возможно) простая проблема с boost :: lexical_cast с составными типами (в моем случае std::vector.

Моя первая версия шаблонизированной функции строкового преобразования была следующей

template <typename T>
std::string stringiy(const T &t)
{
std::ostringstream o;
o<< t;
return o.str();
}

и рабочий пример был следующим:

vector<int> x(10,-3;
cout << stringify<vector<int> >(x) << endl;

с выходом "-3-3-3-3-3-3-3-3"~ но из соображений производительности я хочу использовать boost::lexical_cast

Теперь я изменил реализацию функции:

template <typename T>
std::string stringiy(const T &t)
{
   return boost::lexical_cast<string>(t);
}

, хотя этот метод работает для встроенных типов, он перестает работать для случаев, подобных предыдущему, когда он используется для std::vector

Проблема остается (не компилируется), если создать специализированный шаблон для векторов

template <typename T>
std::string stringiy(const std::vector<T> &t)
{
     vector<string> strret = num2str(t);
     string r;
     for ( vector<string>::iterator iter = strret.begin(); iter!=strret.end(); ++iter )
    r.append(*iter);
     return r;
}

Есть предложения?

g ++ - 4.5, Ubuntu 11.10 amd64

In file included from Util.h:41:0,
                 from testLexicalCast.cpp:49:
/usr/include/boost/lexical_cast.hpp: In member function ‘bool boost::detail::lexical_stream<Target, Source, Traits>::operator<<(const Source&) [with Target = std::basic_string<char>, Source = std::vector<double>, Traits = std::char_traits<char>]’:
/usr/include/boost/lexical_cast.hpp:1151:13:   instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<Source>::param_type, CharT*, size_t) [with Target = std::basic_string<char>, Source = std::vector<double>, bool Unlimited = true, CharT = char, typename boost::call_traits<Source>::param_type = const std::vector<double>&, size_t = long unsigned int]’
/usr/include/boost/lexical_cast.hpp:1174:77:   instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char>, Source = std::vector<double>]’
Util.h:211:43:   instantiated from ‘std::string util::stringify(const T&) [with T = std::vector<double>, std::string = std::basic_string<char>]’
testLexicalCast.cpp:72:53:   instantiated from here
/usr/include/boost/lexical_cast.hpp:595:48: error: no match for ‘operator<<’ in ‘((boost::detail::lexical_stream<std::basic_string<char>, std::vector<double>, std::char_traits<char> >*)this)->boost::detail::lexical_stream<std::basic_string<char>, std::vector<double>, std::char_traits<char> >::stream << input’
/usr/include/c++/4.5/ostream:108:7: note: candidates are: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:117:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/include/c++/4.5/ostream:127:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:165:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:169:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:173:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:91:5: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.5/ostream:180:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:105:5: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.5/ostream:191:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:200:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:204:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:209:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:213:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:221:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:225:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:119:5: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
make: *** [testLexicalCast.o] Error 1

Ответы [ 2 ]

6 голосов
/ 13 декабря 2011

Вы, вероятно, объявили свои operator<< для векторов в глобальном пространстве имен, и boost::lexical_cast будет искать только в пространстве имен boost:: и в пространстве имен std::, где определено ostream.Попробуйте добавить свою перегрузку в пространство имен std::, поместив объявление и определение operator<< в namespace std { ... }.

1 голос
/ 13 декабря 2011

Я, может быть, далеко отсюда, но лично я думаю, что вам было бы лучше вместо того, чтобы пытаться упорядочить контейнер, используя метод 'serialize' для всех контейнеров.

РЕДАКТИРОВАТЬ: ИСПРАВЛЕНО

template<typename Container>
   std::string serialize_container(const Container& container)
{
     std::stringstream ss;
     std::copy(container.begin(), container.end(),    
              std::ostream_iterator<typename Container::value_type>(ss,"-"));
     return ss.str();    
}

РЕДАКТИРОВАТЬ: если вы хотите использовать вектор с stringify, вы делаете это следующим образом.

template <typename T, typename A=void>
std::string stringiy(const T& t)
{
    //...
} 

template <typename T, typename A>
std::string stringiy(const std::vector<T, A>& container)
{
     std::stringstream ss;

     //if << is overloaded for vector
     ss << container;

     /*//else
     std::copy(container.begin(), container.end(),
               std::ostream_iterator<T>(ss,"-"));    
     //        std::ostream_iterator<typename Container::value_type>(ss,"-"));
     */
     return ss.str();    
}

Н.Б. не проверял, но это суть.

...