Noexcept expression vs type properties

I am studying the use of conditional noexcept and noexcept facing this problem. Suppose I have a class:

 template<typename T> class Wrapper { public: Wrapper(T&& value) noexcept(/* ??? */) : value_(std::move(value)) {} private: T value_; }; 

For the part /* ??? */ /* ??? */ I thought we could use either noexcept(T(std::move(value))) or std::is_nothrow_move_constructible<T>::value until I came across this .

Therefore, if I use noexcept(noexcept(T(std::move(value)))) , strictly speaking, I say that "this constructor is noexcept iff, building and destroying a T is noexcept "?

Although the destructors that throw should be set on fire and burned.

+6
source share
1 answer

Good question, see also discussion of this language defect . From its name, it becomes clear that std::is_nothrow_move_constructible<T>::value should relate only to constructivity from rvalue (but in practice it can also relate to destruction), and noexcept(T(std::move(value))) always applies to both construction and destruction.

So, in your case, the most economical way to avoid an unresolved problem with std::is_nothrow_move_constructible is to use a new placement, avoiding the problem with std::bad_alloc (mentioned in Chris Beck's comment), and similarly, use the T descriptor for the wrapper destructor.

 template<typename T> class Wrapper { public: Wrapper(T&& value) noexcept(new(nullptr) T(std::move(value))) : value_(std::move(value)) {} ~Wrapper() noexcept(noexcept(value_.T::~T())) {} private: T value_; }; 
+6
source

All Articles