[lexical_cast] Error casting void* to std::string

I did a search on the mailing list and found nothing, but apologies if I missed it. This bug seems to be new to 1.48. Compiling the following code in MSVC2010: #include <boost/lexical_cast.hpp> #include <string> int main() { void* ptr = 0; boost::lexical_cast<std::string>(ptr); } Results in an error, pointing to line 1738 in boost\lexical_cast.hpp, stating: 'removed_ptr_t': illegal sizeof operand. ('removed_ptr_t' is, however, defined a few lines above.) I see nothing wrong with the code from a C++ point-of-view, so I'm guessing this is another MSVC bug. Is this known? I don't see an immediate permanent workaround. A client-side workaround exists by introducing this small class: struct print_ptr { explicit print_ptr(void* ptr) : mPtr(ptr) {} friend std::ostream& operator<<(std::ostream& sink, print_ptr printPtr) { return sink << printPtr.mPtr; } private: void* mPtr; }; And using: boost::lexical_cast<std::string>(print_ptr(ptr)); -- GMan, Nick Gorski

On Nov 23, 2011, at 1:36 PM, GMan wrote:
I did a search on the mailing list and found nothing, but apologies if I missed it. This bug seems to be new to 1.48. Compiling the following code in MSVC2010:
#include <boost/lexical_cast.hpp> #include <string>
int main() { void* ptr = 0; boost::lexical_cast<std::string>(ptr); }
gcc 4.2.1 complains about this code, too: $ g++ -I /Volumes/EyeFive/Marshall/Sources/boost/trunk junk.cpp/Volumes/EyeFive/Marshall/Sources/boost/trunk/boost/lexical_cast.hpp: In static member function ‘static Target boost::detail::lexical_cast_do_cast<Target, Source>::lexical_cast_impl(const Source&) [with Target = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Source = void*]’: /Volumes/EyeFive/Marshall/Sources/boost/trunk/boost/lexical_cast.hpp:1979: instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Source = void*]’ junk.cpp:7: instantiated from here /Volumes/EyeFive/Marshall/Sources/boost/trunk/boost/lexical_cast.hpp:1805: error: invalid application of ‘sizeof’ to a void type Interestingly enough, clang doesn't mind it at all. What do you expect this code to do? -- Marshall Marshall Clow Idio Software <mailto:mclow.lists@gmail.com> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki

On Wed, Nov 23, 2011 at 2:18 PM, Marshall Clow <mclow.lists@gmail.com>wrote:
gcc 4.2.1 complains about this code, too: [...] Interestingly enough, clang doesn't mind it at all.
Ah yes, I see it now; should have before. In the case where Source is void*, removed_ptr_t is void, and sizeof(void) is an error. (Perhaps clang allows sizeof(void) as an extension?) In any case, it seems a specialization/branch is needed for void*, but I don't know the internals of lexical_cast well enough to know what the result of that branch should be in this situation.
What do you expect this code to do?
In my mind, lexical_cast is a nicer/safer/optimized version of a std::stringstream cast. That is, in the case of no errors lexical_cast<R>(x) has the same result as: template <typename S> R lexical_cast_exposition(const S& x) // assumes no errors { std::stringstream ss; ss << x; R y; ss >> y; return y; } In my case, S is void* and R is std::string (and it uses ostream& operator<<(const void*)). -- GMan, Nick Gorski

2011/11/24 GMan <gmannickg@gmail.com>:
In my mind, lexical_cast is a nicer/safer/optimized version of a std::stringstream cast. That is, in the case of no errors lexical_cast<R>(x) has the same result as:
template <typename S> R lexical_cast_exposition(const S& x) // assumes no errors { std::stringstream ss; ss << x;
R y; ss >> y;
return y; }
In my case, S is void* and R is std::string (and it uses ostream& operator<<(const void*)).
Great thanks for reporting this bug. Ticket is already crated(#6132) I`ll try to fix it till next release. Best regards, Antony Polukhin
participants (3)
-
Antony Polukhin
-
GMan
-
Marshall Clow