相关疑难解决方法(0)

LWG2349会有什么影响?

虽然libstdc ++没有,但libc ++确实遵循标准,该标准规定传递ios_base::failbitbasic_istream::exceptions格式化输入没有任何影响.例如这段代码:

istringstream is{"ASD"};    
double foo;

is.exceptions(istream::failbit);

try {
    is >> foo;
    cout << foo << endl;
} catch(ios_base::failure& fail) {
    cout << "ouch\n";
}
Run Code Online (Sandbox Code Playgroud)

会导致:

我对LWG2349的阅读是因为它basic_istream不会抛出任何格式化的输入.

例如,LWG2349建议对标准的27.7.2.3 [istream]/1进行更改,该标准引用了一个错误的失效,该错误会使libc ++行为像libstdc ++.更改以粗体显示,并在下面进行说明:

如果在输入期间抛出除抛出的异常clear()(如果有的话)之外的异常,则ios::badbit*this错误状态下打开.(抛出的异常basic_ios<>::clear()不会被捕获或重新抛出.)如果(exceptions()&badbit) != 0那么异常被重新抛出.

我明白这basic_istream::clear是对错误的格式化输入的反应,所以我误读了LWG2349还是实际上它会停止basic_istream抛出任何错误?

c++ exception istream libstdc++ libc++

7
推荐指数
1
解决办法
1552
查看次数

libc ++ std :: istringstream不会抛出异常.错误?

配置了一个std::istringstreamfailbit设置时抛出异常的时候我没有用libc ++发生异常(这是在linux下使用libc ++编译并在libcxxrt的支持下编译).我想这是libc ++或libcxxrt中的一个错误:

#include <iostream>
#include <sstream>

template<typename T> std::istream &getvalue(std::istream &is, T &value, const T &default_value = T())
{
    std::stringstream ss;
    std::string s;
    std::getline(is, s, ',');
    ss << s;
    if((ss >> value).fail())
        value = default_value;
    return is;
}

int main()
{
    std::string s = "123,456,789";
    std::istringstream is(s);
    unsigned n;

    try
    {
        is.exceptions(std::ios::failbit | std::ios::eofbit);

        getvalue(is, n);
        std::cout << n << std::endl;

        getvalue(is, n);
        std::cout << n << std::endl;

        // Disable EOF exception on last …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer libc++

6
推荐指数
1
解决办法
950
查看次数

标签 统计

c++ ×2

libc++ ×2

exception ×1

istream ×1

language-lawyer ×1

libstdc++ ×1