Search code examples
c++variadic-templatesvariadicnoexceptparameter-pack

Parameter pack inside noexcept specifier


Currently in C++ neither of these are possible, the compiler complains that it expects an expression.

This appears trivial to me, if you're building a tuple like object with a variadic amount of types, how to check if all those types are nothrow default / move / copy constructible?

This looks like an annoying language defect to me.

Any alternatives?

#include <iostream>
#include <type_traits>

template <typename... TYPES>
struct Test1 {
    Test1()
    noexcept(... && (std::is_nothrow_default_constructible_v<TYPES>)) {}
};

template <typename... TYPES>
struct Test2 {
    Test2()
    noexcept(std::conjunction_v<std::is_nothrow_default_constructible<TYPES>, ...>) {}
};

int
main() {

    Test1<int, int, int> test1;
    Test2<int, int, int> test2;

    return 0;
}

Solution

  • Both are possible. You just didn't employ the correct syntax. It's

    noexcept((... && std::is_nothrow_default_constructible_v<TYPES>))
    

    or

    noexcept(std::conjunction_v<std::is_nothrow_default_constructible<TYPES>...>)
    

    The parentheses need to go around the entire fold expression in the first option. And in the second, the comma is superfluous. A comma separated list is already implied by the pack expansion in the second form.