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

可紊 提交于 2019-12-13 04:43:11

问题


#include <vector>
class A
{
    std::vector<int> vec;

    void swap( A & other) noexcept(noexcept(vec.swap(other.vec)))
    {
        vec.swap(other.vec);
    }

};

int main()
{
}

This code compiles under clang(3.4) but not under gcc (4.7.1). Anyone can tell me what I am doing wrong?

EDIT

gcc error message is :

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

回答1:


As a work around, you may use (which works for gcc 4.7.1, gcc 4.8.1 and clang 3.4):

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

or

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

I think the problem is other.vec...



来源:https://stackoverflow.com/questions/22115944/how-to-declare-noexcept-if-only-a-member-function-of-an-attribute-is-noexcept

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!