命名空间
变体
操作

std::system_error::operator=

来自 cppreference.cn
< cpp‎ | error‎ | system error
 
 
 
 
 
system_error& operator=( const system_error& other ) noexcept;
(自 C++11 起)

将内容赋值为 other 的内容。如果 *this 和 other 都具有动态类型 std::system_error,则赋值后 std::strcmp(what(), other.what()) == 0。

[编辑] 参数

other - 要赋值的另一个 system_error 对象

[编辑] 返回值

*this

[编辑] 示例

#include <cassert>
#include <cstring>
#include <iostream>
#include <system_error>
 
void print(const std::system_error& e)
{
    std::cout << "code:    [" << e.code() << "]\n"
                 "message: [" << e.code().message() << "]\n"
                 "what:    [" << e.what() << "]\n\n";
}
 
int main()
{
    std::system_error e1(EDOM, std::generic_category(), "Error info #1");
    print(e1);
 
    std::system_error e2(EIO, std::system_category(), "Error info #2");
    print(e2);
 
    e1 = e2;
    assert(std::strcmp(e1.what(), e2.what()) == 0);
    print(e1);
}

可能的输出

code:    [generic:33]
message: [Numerical argument out of domain]
what:    [Error info #1: Numerical argument out of domain]
 
code:    [system:5]
message: [Input/output error]
what:    [Error info #2: Input/output error]
 
code:    [system:5]
message: [Input/output error]
what:    [Error info #2: Input/output error]