lz96
lz96

Reputation: 2998

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

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?

Upvotes: 1

Views: 89

Answers (1)

Alejandro
Alejandro

Reputation: 3082

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.

Upvotes: 2

Related Questions