Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 导出streambuf还是基本流?_C++_Ostream_Ostringstream_Streambuf - Fatal编程技术网

C++ 导出streambuf还是基本流?

C++ 导出streambuf还是基本流?,c++,ostream,ostringstream,streambuf,C++,Ostream,Ostringstream,Streambuf,我想导出一个stringstream,这样我就可以使用操作符您可能可以通过执行以下操作来简化它: class error_builder { public: error_builder(const std::string& pMsg = "") { mMsg << pMsg; } ~error_builder(void) { throw std::runtime_error(mMsg.str());

我想导出一个stringstream,这样我就可以使用操作符您可能可以通过执行以下操作来简化它:

class error_builder
{
public:
    error_builder(const std::string& pMsg = "")
    {
        mMsg << pMsg;
    }

    ~error_builder(void)
    {
        throw std::runtime_error(mMsg.str());
    }

    template <typename T>
    error_builder& operator<<(const T& pX)
    {
        mMsg << pX;

        return *this;
    }

private:
    std::stringstream mMsg;    
};


error_builder("some text") << " more text " << 42 << std::endl;
类错误\u生成器
{
公众:
错误(const std::string&pMsg=“”)
{

mMsg我将在这里再次小跑出我最喜欢的宏:

#define ATHROW( msg )                                               \
{                                                                   \
    std::ostringstream os;                                          \
    os << msg;                                                      \
    throw ALib::Exception( os.str(), __LINE__, __FILE__ );          \
}                                                                   \
#定义ATHROW(msg)\
{                                                                   \
std::ostringstream os\

操作系统GMan的解决方案中缺少一些操作符

class error {
   public:
   explicit error(const std::string& m = "") :
          msg(m, std::ios_base::out | std::ios_base::ate)
   {}

   ~error() {
      if(!std::uncaught_exception()) {
         throw std::runtime_error(msg.str());
      }
   }

   template<typename T>
   error& operator<<(const T& t) {
      msg << t;
      return *this;
   }

   error& operator<<(std::ostream& (*t)(std::ostream&)) {
      msg << t;
      return *this;
   }
   error& operator<<(std::ios& (*t)(std::ios&)) {
      msg << t;
      return *this;
   }
   error& operator<<(std::ios_base& (*t)(std::ios_base&)) {
      msg << t;
      return *this;
   }
   private:
   std::ostringstream msg;
};
类错误{
公众:
显式错误(const std::string&m=“”):
msg(m,std::ios_base::out | std::ios_base::ate)
{}
~error(){
如果(!std::uncaught_exception()){
抛出std::runtime_错误(msg.str());
}
}
模板

error&operator我通常只创建自己的异常类。您只需重写
what()
,并可以提供任意数量的构造函数。若要生成错误消息,只需使用vasprintf(如果可用)或上述std::ostringstream

下面是一个例子:

class CustomException : public std::exception {
private:
    const std::string message;
public:
    CustomException(const std::string &format, ...) {
        va_list args;
        va_start(args, format);
        char *formatted = 0;
        int len = vasprintf(&formatted, format.c_str(), args);
        if (len != -1) {
            message = std::string(formatted);
            free(formatted);
        } else {
            message = format;
        }
        va_end(args);
    }
    const char *what() const {
        return message.c_str();
    }
};

如果没有vasprintf,也可以在堆栈上使用vsnprintf和缓冲区…

我不会真正抛出const char*s。这只是一个概念。你不应该在析构函数()中抛出异常。@Helltone:这是一个异常。了解通常不应该抛出的原因(这里的关键字)throw:在堆栈展开过程中,如果两个异常处于活动状态,则应用程序将终止。这里显然不是这种情况,因为它打算立即抛出(公平地说,流可能会失败并抛出,但是meh)编辑:好吧,不管怎么说,已经解决了,就这样。@Helltone:streams的异常在默认情况下是关闭的,stream异常是
错误生成器
生命周期内可能抛出的少数事件之一。否则,唯一可能抛出的是
std::bad\u alloc
en
std::terminate
可能是最好的解决方案。有时会出现错误,宏是最好的解决方案。
ATHROW( "Invalid value: " << x << " should be " << 42 );
class error {
   public:
   explicit error(const std::string& m = "") :
          msg(m, std::ios_base::out | std::ios_base::ate)
   {}

   ~error() {
      if(!std::uncaught_exception()) {
         throw std::runtime_error(msg.str());
      }
   }

   template<typename T>
   error& operator<<(const T& t) {
      msg << t;
      return *this;
   }

   error& operator<<(std::ostream& (*t)(std::ostream&)) {
      msg << t;
      return *this;
   }
   error& operator<<(std::ios& (*t)(std::ios&)) {
      msg << t;
      return *this;
   }
   error& operator<<(std::ios_base& (*t)(std::ios_base&)) {
      msg << t;
      return *this;
   }
   private:
   std::ostringstream msg;
};
class CustomException : public std::exception {
private:
    const std::string message;
public:
    CustomException(const std::string &format, ...) {
        va_list args;
        va_start(args, format);
        char *formatted = 0;
        int len = vasprintf(&formatted, format.c_str(), args);
        if (len != -1) {
            message = std::string(formatted);
            free(formatted);
        } else {
            message = format;
        }
        va_end(args);
    }
    const char *what() const {
        return message.c_str();
    }
};