Opened 14 years ago

Closed 10 years ago

Last modified 10 years ago

#2558 closed Bugs (fixed)

Unnecessary conversion warning in lexical_cast

Reported by: Yang Zhang <yanghatespam@…> Owned by: Antony Polukhin
Milestone: Boost 1.54.0 Component: lexical_cast
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:

Description

When trying to compile the following against r50058 with g++ -Wconversion:

#include <boost/lexical_cast.hpp>
#include <string>
using namespace boost;
using namespace std;
int main() {
  lexical_cast<string>(3);
  return 0;
}

I get:

/home/yang/work/boost/boost/lexical_cast.hpp: In function ‘CharT* boost::detail::lcast_put_unsigned(T, CharT*) [with Traits = std::char_traits<char>, T = unsigned int, CharT = char]’:
/home/yang/work/boost/boost/lexical_cast.hpp:848:   instantiated from ‘bool boost::detail::lexical_stream_limited_src<CharT, Base, Traits>::operator<<(int) [with CharT = char, Base = boost::detail::lexical_streambuf_fake, Traits = std::char_traits<char>]’
/home/yang/work/boost/boost/lexical_cast.hpp:1142:   instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<Source>::param_type, CharT*, size_t) [with Target = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Source = int, bool Unlimited = false, CharT = boost::lexical_cast::char_type]’
/home/yang/work/boost/boost/lexical_cast.hpp:1166:   instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Source = int]’
blah.cc:6:   instantiated from here
/home/yang/work/boost/boost/lexical_cast.hpp:510: warning: conversion to ‘char’ from ‘int’ may alter its value
/home/yang/work/boost/boost/lexical_cast.hpp:848:   instantiated from ‘bool boost::detail::lexical_stream_limited_src<CharT, Base, Traits>::operator<<(int) [with CharT = char, Base = boost::detail::lexical_streambuf_fake, Traits = std::char_traits<char>]’
/home/yang/work/boost/boost/lexical_cast.hpp:1142:   instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<Source>::param_type, CharT*, size_t) [with Target = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Source = int, bool Unlimited = false, CharT = boost::lexical_cast::char_type]’
/home/yang/work/boost/boost/lexical_cast.hpp:1166:   instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Source = int]’
blah.cc:6:   instantiated from here
/home/yang/work/boost/boost/lexical_cast.hpp:512: warning: conversion to ‘char’ from ‘int’ may alter its value
/home/yang/work/boost/boost/lexical_cast.hpp:536: warning: conversion to ‘char’ from ‘int’ may alter its value

It seems this is because the header is using CHAR_MAX as-is and expecting it to be a char.

Change History (7)

comment:1 by nasonov, 13 years ago

Resolution: worksforme
Status: newclosed

Not reproducible neither with gcc 3.4.6 nor 4.1.3 on NetBSD 5.99.17 (-current). Please submit a patch.

comment:2 by anonymous, 10 years ago

Milestone: Boost 1.38.0Boost 1.53.0
Resolution: worksforme
Status: closedreopened

This above code is either broken again or still broken with g++ 4.7.

$ g++-fsf-4.7 -I/sw/include/ -Wconversion Foo.cpp 
In file included from Foo.cpp:1:0:
/sw/include/boost/lexical_cast.hpp: In instantiation of 'CharT* boost::detail::lcast_put_unsigned(T, CharT*) [with Traits = std::char_traits<char>; T = unsigned int; CharT = char]':
/sw/include/boost/lexical_cast.hpp:860:13:   required from 'bool boost::detail::lexical_stream_limited_src<CharT, Base, Traits>::operator<<(int) [with CharT = char; Base = boost::detail::lexical_streambuf_fake; Traits = std::char_traits<char>]'
/sw/include/boost/lexical_cast.hpp:1151:13:   required from 'Target boost::detail::lexical_cast(typename boost::call_traits<Source>::param_type, CharT*, std::size_t) [with Target = std::basic_string<char>; Source = int; bool Unlimited = false; CharT = char; typename boost::call_traits<Source>::param_type = const int; std::size_t = long unsigned int]'
/sw/include/boost/lexical_cast.hpp:1174:77:   required from 'Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char>; Source = int]'
Foo.cpp:6:25:   required from here
/sw/include/boost/lexical_cast.hpp:522:72: warning: conversion to 'char' from 'int' may alter its value [-Wconversion]
/sw/include/boost/lexical_cast.hpp:524:74: warning: conversion to 'char' from 'int' may alter its value [-Wconversion]
/sw/include/boost/lexical_cast.hpp:548:25: warning: conversion to 'char' from 'int' may alter its value [-Wconversion]

comment:3 by anonymous, 10 years ago

That was 1.46.1 by the way.

comment:4 by Antony Polukhin, 10 years ago

Owner: changed from nasonov to Antony Polukhin
Status: reopenednew

comment:5 by Antony Polukhin, 10 years ago

(In [82695]) Fixed as many -Wconversion warnings in lexical cast and tests as possible (refs #2558)

comment:6 by Antony Polukhin, 10 years ago

Resolution: fixed
Status: newclosed

(In [83114]) Merge from trunk:

  • Silence some buggy VS warnings (fixes #7949)
  • Fix -Wconversion warnings (fixes #2558)
  • Added support for int128 conversions (fixes #7909)

comment:7 by Antony Polukhin, 10 years ago

Milestone: Boost 1.53.0Boost 1.54.0
Note: See TracTickets for help on using tickets.