0
#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’
4

1 回答 1

3

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

于 2014-03-01T15:26:45.047 回答