如何声明 noexcept 如果只有属性的成员函数是 noexcept

How to declare noexcept if only a member function of an attribute is noexcept?

本文关键字:noexcept 属性 成员 函数 如果 何声明 声明      更新时间:2023-10-16
#include <vector>
class A
{
    std::vector<int> vec;
    void swap( A & other) noexcept(noexcept(vec.swap(other.vec)))
    {
        vec.swap(other.vec);
    }
};
int main()
{
}

这段代码在 clang(3.4) 下编译,但不在 gcc (4.7.1) 下编译。谁能告诉我我做错了什么?

编辑

GCC 错误消息是 :

error: invalid use of incomplete type ‘class A’
error: forward declaration of ‘class A’

作为解决方法,您可以使用(适用于 gcc 4.7.1、gcc 4.8.1 和 clang 3.4):

void swap(A& other) noexcept(noexcept(std::declval<decltype(A::vec)&>().swap(std::declval<decltype(A::vec)&>())))

void swap(A& other) noexcept(noexcept(vec.swap(vec)))

我认为问题other.vec...