C++ C+中的字符串构建+;例外情况’;这是什么

C++ C+中的字符串构建+;例外情况’;这是什么,c++,multithreading,exception,string-building,C++,Multithreading,Exception,String Building,声明一个私有静态ostringstream。这个线安全吗?如果两个线程同时抛出(和catch,并记录what())异常,这是否可靠?如果我在本地声明ostringstream,比如: virtual const char* what() const throw() { std::ostringstream cnvt.str( "" ); cnvt << runtime_error::what() << ": " << getNumerator(

声明一个
私有静态ostringstream
。这个线安全吗?如果两个线程同时抛出(和catch,并记录
what()
)异常,这是否可靠?如果我在本地声明
ostringstream
,比如:

virtual const char* what() const throw()
{
    std::ostringstream cnvt.str( "" );
    cnvt << runtime_error::what() << ": " << getNumerator()
         << " / " << getDenominator();
    return cnvt.str().c_str();
}
virtual const char*what()const throw()
{
std::ostringstream cnvt.str(“”);
cnvt不。一点也不安全(我觉得效率很低,可以单独用
std::string
来做)。
为了安全起见,将
ostringstream
声明为
thread\u local

静态线程\u本地ostringstream cnvt;

此外,您应该使
cnvt
将字符串输出到某个成员字符串,以避免返回悬空指针

class DivideByZeroException: public runtime_error {
public:

  DivideByZeroException(int x, int y)
    : runtime_error( "division by zero" ), numerator( x ), denominator( y )
    {}

  virtual const char* what() const throw()
  {
    cnvt.str( "" );

    cnvt << runtime_error::what() << ": " << getNumerator()
         << " / " << getDenominator();

    error = cnvt.str();
    return error.c_str();
  } 

   /*...*/

   private:
     std::string error;
     int numerator;
     int denominator;

   static thread_local ostringstream cnvt;
};
class DivideByZeroException:公共运行时\u错误{
公众:
除零异常(整数x,整数y)
:运行时错误(“除零”)、分子(x)、分母(y)
{}
虚拟常量char*what()常量throw()
{
cnvt.str(“”);
cnvt不。一点也不安全(我觉得效率很低,可以单独用
std::string
来做)。
为了安全起见,将
ostringstream
声明为
thread\u local

静态线程\u本地ostringstream cnvt;

此外,您应该使
cnvt
将字符串输出到某个成员字符串,以避免返回悬空指针

class DivideByZeroException: public runtime_error {
public:

  DivideByZeroException(int x, int y)
    : runtime_error( "division by zero" ), numerator( x ), denominator( y )
    {}

  virtual const char* what() const throw()
  {
    cnvt.str( "" );

    cnvt << runtime_error::what() << ": " << getNumerator()
         << " / " << getDenominator();

    error = cnvt.str();
    return error.c_str();
  } 

   /*...*/

   private:
     std::string error;
     int numerator;
     int denominator;

   static thread_local ostringstream cnvt;
};
class DivideByZeroException:公共运行时\u错误{
公众:
除零异常(整数x,整数y)
:运行时错误(“除零”)、分子(x)、分母(y)
{}
虚拟常量char*what()常量throw()
{
cnvt.str(“”);
cnvt
what()
是构建字符串IMHO的错误位置(尽管对此有不同的看法)

std::runtime\u error
已经包含一个字符串,所以让我们使用这个字符串

#include <stdexcept>
#include <string>

struct DivideByZero : std::runtime_error
{

    DivideByZero(int x, int y)
    : std::runtime_error( make_message(x,y) )
    {}

private:
    static std::string make_message(int x, int y)
    {
        return std::string("division by zero: " + std::to_string(x) + '/' + std::to_string(y));
    }

};
#包括
#包括
结构DivideByZero:std::runtime\u错误
{
除以零(整数x,整数y)
:std::运行时错误(生成消息(x,y))
{}
私人:
静态标准::字符串生成消息(intx,inty)
{
返回std::string(“除零:”+std::to_string(x)+'/'+std::to_string(y));
}
};
what()
是构建字符串IMHO的错误位置(尽管对此有不同的看法)

std::runtime\u error
已经包含一个字符串,所以让我们使用这个字符串

#include <stdexcept>
#include <string>

struct DivideByZero : std::runtime_error
{

    DivideByZero(int x, int y)
    : std::runtime_error( make_message(x,y) )
    {}

private:
    static std::string make_message(int x, int y)
    {
        return std::string("division by zero: " + std::to_string(x) + '/' + std::to_string(y));
    }

};
#包括
#包括
结构DivideByZero:std::runtime\u错误
{
除以零(整数x,整数y)
:std::运行时错误(生成消息(x,y))
{}
私人:
静态标准::字符串生成消息(intx,inty)
{
返回std::string(“除零:”+std::to_string(x)+'/'+std::to_string(y));
}
};

在代码中,返回指向超出范围的字符串的指针,这将导致未定义的行为。在代码中,返回指向超出范围的字符串的指针,这将导致未定义的行为。