How can I declare a move constructor of a wrapper type X<T> noexcept depending on is_nothrow_move_constructible<T>?
Asked Answered
M

1

9

Assume I have a wrapper type

template <typename T>
struct X {/*..*/};

and I cannot just X(X&&) = default because I have to do non-trivial stuff there.

However, I want it to be noexcept but only in case that T(T&&) is noexcept. This can be tested with ::std::is_nothrow_move_constructible.

I'm at a loss how to conditionally enable one version of the constructor or the other depending on a constexpr. I suppose there could be a way to use SFINAE, but I don't see how to apply it to ctors.

Multiped answered 24/11, 2016 at 12:35 Comment(0)
R
10

The noexcept specifier accepts any boolean constant expression, so you can but your type trait check in there directly:

template <typename T>
struct X {
    X(X&&) noexcept(std::is_nothrow_move_constructible<T>::value) {}
}; 
Revivalism answered 24/11, 2016 at 12:39 Comment(2)
Ooooookay, that was easy. Cheers. (I'll accept in an hour)Multiped
I'm using this for the following, just just in case anyone wonders ... codereview.stackexchange.com/q/147999/7189Multiped

© 2022 - 2024 — McMap. All rights reserved.