How to find out whether an assignment operator of T in a function template throws an exception?

88 views Asked by At

Here is my function template:

template <typename T>
void f(vector<T> &a) noexcept(noexcept( /* ??? */ ))

I want to specify this function will not throw an exception given that the assignment operator = of T has noexcept specification. Is there a way to do this?

1

There are 1 answers

2
Alejandro On BEST ANSWER

You can do that with this:

template<typename T> 
void f(std::vector<T>& a) noexcept(std::is_nothrow_copy_assignable<T>::value)
{...}

It places a condition on the noexcept if copy-assigning T values is itself declared noexcept. You can further this into also taking account move-assigning T.