Which of them is the correct one and why?
I think it's the first one because Ts
already have the &&
or const &
associated with the type but I like to be sure and there really isn't that many examples of this particular case of noexcept
.
template <typename T>
struct Test {
T test;
// A
template <typename... Ts>
Test(Ts &&... ts) noexcept(std::is_nothrow_constructible_v<T, Ts...>)
: test(std::forward<Ts>(ts)...) {
}
// B
template <typename... Ts>
Test(Ts &&... ts) noexcept(std::is_nothrow_constructible_v<T, Ts &&...>)
: test(std::forward<Ts>(ts)...) {
}
};
I prefer option C. You can use the noexcept
operator in the noexcept specifier and have it evaluate the call for you. I find this a lot easier to read an understand since you use the expression you want to do for exception specifier. In you case that would look like
template <typename T>
struct Test {
T test;
// C
template <typename... Ts>
Test(Ts &&... ts) noexcept(noexcept(T(std::forward<Ts>(ts)...)))
: test(std::forward<Ts>(ts)...) {
}
};
And now the code says Test(Ts &&... ts)
is noexcept if T(std::forward<Ts>(ts)...)
is noexcept.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With