如果只有一个属性的成员函数是noexcept,如何声明noexcept? [英] How to declare noexcept if only a member function of an attribute is noexcept?

查看:471
本文介绍了如果只有一个属性的成员函数是noexcept,如何声明noexcept?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #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)下编译。任何人都可以告诉我我在做什么错误?



EDIT



<消息是:

 错误:无效使用不完全类型'A类'
错误: '


解决方案

为gcc 4.7.1,gcc 4.8.1和clang 3.4工作):

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

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

other.vec ...


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

解决方案

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...

这篇关于如果只有一个属性的成员函数是noexcept,如何声明noexcept?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆