声明 POD 类型会抛出异常吗?

Can declaring POD types throw an exception?

当我声明一个 condition_variable 时,它可能会抛出 std::system_error.

但是当我声明 POD 类型(例如 intdoublefloat)时呢?

喜欢下面的代码:

int main()
{
    //do something
    int i;    //will here throw exception?
}

如果声明一个POD类型可能会抛出异常,我如何保证

void test() noexcept
{
    //do something
    int i;
}

noexcept吗?

But how about declare a fundamental type (e.g., int, double or float)?

声明 POD type 对象不会引发异常。

non-POD 类型的构造函数可以抛出异常。只有这些类型的 documents/source 代码可以帮助您确定特定类型是否会发生这种情况。

可以抛出异常的是std::condition_variableconstructorintdouble 等原始类型没有任何构造函数。他们只是为他们分配了一些堆栈 space ,仅此而已,如果您初始化变量,还会写入一个值。这可能导致异常的唯一方法是如果您溢出堆栈并且随后的未定义行为恰好抛出一个。

POD 类型通常使用初始化表达式而不是构造函数进行初始化。与构造函数一样,初始化器可以抛出异常。但是,如果您既没有构造函数也没有初始化程序,则没有与定义关联的代码,因此也没有可能抛出该代码。