Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-03-22 02:54:19

>From: "David Abrahams" <dave_at_[hidden]>

> Terje Slettebø <tslettebo_at_[hidden]> writes:
> >> C:\Program Files\Boost\boost_1_30_0\boost/lexical_cast.hpp(74) :
> > C4512: 'no_lexical_conversion<class std::basic_string<char,struct
> > std::char_traits<char>,class std::allocator<char> >,long>' : assignment
> > operator could not be generated
> >
> > This is due to that it stores a const std::string object, describing the
> > exception
> Are you saying that you have defined an exception with a std::string
> member? That's VERY bad! Throwing that exception can lead directly
> to termination!

You mean if the exception itself throws during construction or copying?

I've tried the program below on Intel C++ 7.0 and MSVC 6, and I haven't got
it to call terminate(). It may be that it doesn't handle exceptions
correctly, though.

#include <iostream>
#include <string>
#include <stdexcept>

class Exception : public std::runtime_error
  Exception() : std::runtime_error("Exception")
    { throw std::runtime_error("Construction"); }

  Exception(const Exception &) : std::runtime_error("Exception (copied)")
    { throw std::runtime_error("Copy"); }

int main()
    throw Exception();
  catch(const std::exception &e)
    std::cout << "Exception - " << e.what() << '\n';
    std::cout << "Unknown exception\n";

As it stands, it prints "Exception - Constructor", as it throws an exception
in the constructor of the Exception exception. If the throw-statement in the
constructor is commented out, it prints "Exception - Exception", apparently
not invoking the copy constructor when throwing the exception (which it's
allowed to).

> What's wrong with char const*?

You mean storing the string as a character array? Sure, that's possible, and
I see that STLPort do it, and it's probably safer, as you say. It does mean
you have to specify the maximum string length in advance, though. As
"no_lexical_conversion" what() prints out the source and target types, it
may truncate long type names.



Boost list run by bdawes at, gregod at, cpdaniel at, john at