【问题标题】:How to declare noexcept if only a member function of an attribute is noexcept?如果只有属性的成员函数是 noexcept,如何声明 noexcept?
【发布时间】:2014-04-02 16:22:02
【问题描述】:
#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’

【问题讨论】:

  • 错误信息,好吗?

标签: c++ gcc c++11 clang noexcept


【解决方案1】:

作为一种变通方法,您可以使用(适用于 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...

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2020-10-24
    • 2014-02-23
    • 2012-10-14
    • 2016-06-26
    • 2015-10-17
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多