Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Noexcept variadic is_nothrow_constructible_v

Tags:

c++

noexcept

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)...) {
    }
};
like image 835
João Pires Avatar asked Sep 14 '25 06:09

João Pires


1 Answers

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.

like image 177
NathanOliver Avatar answered Sep 15 '25 20:09

NathanOliver