Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does one 'copy ctor' is noexcept, while the other is noexcept(false)?

18 views
Skip to first unread message

fl

unread,
Jul 16, 2015, 12:03:33 AM7/16/15
to
Hi, C++ experts:

From the following description,
...........
The result is false if the expression contains at least one of the following potentially evaluated constructs:
1. call to any type of function that does not have non-throwing exception
specification, unless it is a constant expression.
2. throw expression.
3. dynamic_cast expression when the target type is a reference type, and
conversion needs a run time check
4. typeid expression when argument type is polymorphic class type
.......

I cannot understand why the copy ctor in class U has a noexcept(false) in
the below example code.

Could you tell me the reason?

Thanks,



............
#include <iostream>
#include <utility>
#include <vector>

void may_throw();
void no_throw() noexcept;
auto lmay_throw = []{};
auto lno_throw = []() noexcept {};
class T{
public:
~T(){} // dtor prevents move ctor
// copy ctor is noexcept
};
class U{
public:
~U(){} // dtor prevents move ctor
// copy ctor is noexcept(false)
std::vector<int> v;
};
class V{
public:
std::vector<int> v;
};

Öö Tiib

unread,
Jul 16, 2015, 4:12:07 AM7/16/15
to
On Thursday, 16 July 2015 07:03:33 UTC+3, fl wrote:
> Hi, C++ experts:
>
> From the following description,
> ...........
> The result is false if the expression contains at least one of the following potentially evaluated constructs:
> 1. call to any type of function that does not have non-throwing exception
> specification, unless it is a constant expression.
> 2. throw expression.
> 3. dynamic_cast expression when the target type is a reference type, and
> conversion needs a run time check
> 4. typeid expression when argument type is polymorphic class type
> .......
>
> I cannot understand why the copy ctor in class U has a noexcept(false) in
> the below example code.
>
> Could you tell me the reason?

Default copy constructor just does copy-construct all bases and
members. U's member v is vector whose copy constructor may throw.
Therefore U's default copy constructor may also throw what v's
constructor throws and so it can't be 'noexcept'.

0 new messages